Skip to content

Commit 11da76e

Browse files
committed
feat(search): replace writerside non-symantic elements for better internal search index
refactor(parsers): replace div.code-block elements with code tags
1 parent f2ebf70 commit 11da76e

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

scripts/doindex/parsers/writerside.mjs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function getBreadcrumbs(article) {
1919
*/
2020
function dropIrrelevantSections(article) {
2121
article.find([
22-
'.chapter:has(#what-s-next)', '.chapter:has(#next-step)',
22+
'.chapter:has(#what-s-next)', '.chapter:has(#next-step)', '.chapter:has(#next-steps)',
2323
'.chapter:has(#learn-more)', '.chapter:has(#leave-feedback)'
2424
].join(', ')).remove();
2525
}
@@ -39,6 +39,41 @@ function replaceMedia(article, pageUrl) {
3939
}
4040
}
4141

42+
/**
43+
* @param {import('cheerio').Element} node
44+
*/
45+
function cloneAttrsString(node) {
46+
return Object.entries((node.attribs || {})).map(([key, value]) => {
47+
const val = typeof value === 'string' ? `="${value}"` : '';
48+
return `${key}${val}`;
49+
}).join(' ');
50+
}
51+
52+
/**
53+
* @param {import('cheerio').Cheerio} article
54+
* @returns {void}
55+
*/
56+
function replaceWRSSemantic(article) {
57+
const listStrong = article.find('span.control');
58+
59+
for (let i = 0, length = listStrong.length; i < length; i++) {
60+
const strong = listStrong.eq(i);
61+
const node = listStrong[i];
62+
const newNode = `<b ${cloneAttrsString(node)}>${strong.html()}</b>`;
63+
strong.replaceWith(newNode);
64+
}
65+
66+
67+
const listCode = article.find('div.code-block');
68+
69+
for (let i = 0, length = listCode.length; i < length; i++) {
70+
const code = listCode.eq(i);
71+
const node = listStrong[i];
72+
const newNode = `<code ${cloneAttrsString(node)}>${code.html()}</code>`;
73+
code.replaceWith(newNode).addClass('code');
74+
}
75+
}
76+
4277
/**
4378
* @param {import('cheerio').Cheerio} article
4479
* @returns {void}
@@ -162,6 +197,7 @@ async function docs($, url, data) {
162197

163198
dropUiElements($article);
164199
dropIrrelevantSections($article);
200+
replaceWRSSemantic($article);
165201
replaceMedia($article, pageUrl.toString());
166202

167203
const breadcrumbs = getBreadcrumbs($body);

0 commit comments

Comments
 (0)