Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 48060f6

Browse files
committed
checkpoint
- not functional, parse line changes in progress
1 parent d2887c4 commit 48060f6

File tree

1 file changed

+102
-15
lines changed

1 file changed

+102
-15
lines changed

scripts/fix-anchors/app.js

Lines changed: 102 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,90 @@ const redirectsFile = path.join(topDir, 'config', 'redirects.json');
1414

1515
const firmwareMdPath = path.join(contentDir, 'reference', 'device-os', 'firmware.md');
1616

17+
let anchors = {};
18+
let sections = ['', ''];
19+
20+
function parseLine(line, lineNum) {
21+
let parsed = {};
22+
23+
if (line.startsWith('##')) {
24+
parsed.isHeader = true;
25+
26+
const m1 = line.match(/^(#+) /);
27+
parsed.level = m1[1].length;
28+
const spaceIndex = line.indexOf(' ');
29+
30+
parsed.origPrefix = line.substring(0, spaceIndex + 1);
31+
parsed.origTitle = line.substring(spaceIndex + 1).trim().replace(/&/g, '&');
32+
33+
const m2 = line.match(/ - (.+)$/);
34+
if (m2) {
35+
parsed.origClassName = m2[1];
36+
}
37+
38+
parsed.shortTitle = parsed.origTitle;
39+
{
40+
const offset1 = parsed.shortTitle.indexOf('[');
41+
if (offset1 > 0) {
42+
parsed.shortTitle = parsed.shortTitle.substring(0, offset1).trim();
43+
}
44+
const offset2 = parsed.shortTitle.indexOf('(');
45+
if (offset2 > 0) {
46+
parsed.shortTitle = parsed.shortTitle.substring(0, offset2).trim();
47+
}
48+
}
49+
50+
if (parsed.level < sections.length) {
51+
sections = sections.slice(0, parsed.level);
52+
}
53+
sections[parsed.level] = parsed.shortTitle;
54+
55+
if (sections[parsed.level - 1].length > 0) {
56+
parsed.newClassName = sections[parsed.level - 1];
57+
}
58+
59+
parsed.origAnchor = parsed.origTitle.toLowerCase().replace(/<[^>]+>/g, '').replace(/[^\w]+/g, '-');
60+
if (parsed.origAnchor === 'constructor') {
61+
parsed.origAnchor += '-';
62+
}
63+
64+
parsed.origAnchorNoTrailingDash = parsed.origAnchor.replace(/[-]+$/g, '');
65+
66+
if (typeof anchors[parsed.origAnchor] != 'undefined') {
67+
anchors[parsed.origAnchor].push(lineNum);
68+
console.log('non-unique anchor', {origAnchor:parsed.origAnchor, level:parsed.level, section: sections[parsed.level - 1], lines:anchors[parsed.origAnchor], });
69+
parsed.uniqueAnchor = parsed.origAnchor + '-' + (anchors[parsed.origAnchor].length + 1);
70+
}
71+
else {
72+
anchors[parsed.origAnchor] = [lineNum];
73+
parsed.uniqueAnchor = parsed.origAnchor;
74+
}
75+
76+
if (typeof parsed.origClassName == 'undefined') {
77+
if (sections[parsed.level - 1].length > 0) {
78+
parsed.newTitle = parsed.origTitle + ' - ' + sections[parsed.level - 1];
79+
parsed.newAnchor = parsed.newTitle.toLowerCase().replace(/<[^>]+>/g, '').replace(/[^\w]+/g, '-');
80+
}
81+
}
82+
else {
83+
}
84+
}
85+
else {
86+
parsed.isHeader = false;
87+
}
88+
89+
return parsed;
90+
}
1791

1892
async function run() {
1993
const redirectsJson = JSON.parse(fs.readFileSync(redirectsFile, 'utf8'));
2094

95+
anchors = {};
96+
sections = ['', ''];
97+
2198
const origStr = fs.readFileSync(firmwareMdPath, 'utf8');
2299
let lineArray = [];
23100

24-
let anchors = {};
25-
let sections = ['', ''];
26-
27101
let lineNum = 0;
28102
for(let line of origStr.split('\n')) {
29103
lineNum++;
@@ -33,6 +107,13 @@ async function run() {
33107
break;
34108
}
35109

110+
const parsed = parseLine(line, lineNum);
111+
if (!parsed.isHeader || parsed.level <= 3) {
112+
// Not a header, or a h2 (directory) or a h3 (file)
113+
continue;
114+
}
115+
116+
/*
36117
if (line.startsWith('##')) {
37118
// Any L2 or higher is an an anchor
38119
const m1 = line.match(/^(#+) /);
@@ -77,36 +158,42 @@ async function run() {
77158
78159
const origAnchorNoTrailingDash = origAnchor.replace(/[-]+$/g, '');
79160
80-
/*
81-
if (typeof origClassName != 'undefined' && typeof newClassName != 'undefined' && origClassName != newClassName) {
82-
console.log('class name changed', {origClassName, newClassName});
161+
let uniqueAnchor;
162+
163+
if (typeof anchors[origAnchor] != 'undefined') {
164+
anchors[origAnchor].push(lineNum);
165+
console.log('non-unique anchor', {origAnchor, level, section: sections[level - 1], lines:anchors[origAnchor], });
166+
uniqueAnchor = origAnchor + '-' + (anchors[origAnchor].length + 1);
83167
}
84-
*/
168+
else {
169+
anchors[origAnchor] = [lineNum];
170+
uniqueAnchor = origAnchor;
171+
}
172+
85173
86174
if (typeof origClassName == 'undefined') {
87175
if (sections[level - 1].length > 0) {
88176
const newTitle = origTitle + ' - ' + sections[level - 1];
89177
line = origPrefix + newTitle;
90178
console.log('updated title', {newTitle, origTitle, line});
179+
180+
const newAnchor = newTitle.toLowerCase().replace(/<[^>]+>/g, '').replace(/[^\w]+/g, '-');
181+
if (uniqueAnchor != newAnchor) {
182+
183+
}
91184
}
92185
}
93186
else {
94-
if (typeof anchors[origAnchor] != 'undefined') {
95-
anchors[origAnchor].push(lineNum);
96-
console.log('non-unique anchor', {origAnchor, level, section: sections[level - 1], lines:anchors[origAnchor], });
97-
}
98-
else {
99-
anchors[origAnchor] = [lineNum];
100-
}
101187
}
102188
103189
}
190+
*/
104191

105192
lineArray.push(line);
106193
}
107194

108195
// fs.writeFileSync(firmwareMdPath, lineArray.join('\n'));
109-
fs.writeFileSync(redirectsFile, JSON.stringify(redirectsJson, null, 2));
196+
// fs.writeFileSync(redirectsFile, JSON.stringify(redirectsJson, null, 2));
110197
}
111198

112199
run();

0 commit comments

Comments
 (0)