Skip to content

Commit fddbba8

Browse files
committed
refactor(search): trim code text for one-liner with \n
1 parent 11da76e commit fddbba8

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

scripts/doindex/lib/parser.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export async function htmlToText($, list, isFinalNode) {
5656
continue;
5757

5858
if (tag === 'code') {
59-
const text = $(node).text();
59+
const text = $(node).text().trim();
6060
if (!text.includes('\n')) {
6161
contentNodes.push('`' + cleanText(text) + '`');
6262
nodes.push([node.nextSibling, level]);

scripts/doindex/parsers/writerside.mjs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,31 @@ function cloneAttrsString(node) {
5151

5252
/**
5353
* @param {import('cheerio').Cheerio} article
54-
* @returns {void}
54+
* @param {string} selector
55+
* @param {($node: import('cheerio').Cheerio<Element>, attrs: string, content: string) => string} cb
5556
*/
56-
function replaceWRSSemantic(article) {
57-
const listStrong = article.find('span.control');
57+
function replaceNode(article, selector, cb) {
58+
const listStrong = article.find(selector);
5859

5960
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);
61+
const $node = listStrong.eq(i);
62+
const newNode = cb($node, cloneAttrsString(listStrong[i]), $node.html());
63+
$node.replaceWith(newNode);
6464
}
65+
}
6566

67+
/**
68+
* @param {import('cheerio').Cheerio} article
69+
* @returns {void}
70+
*/
71+
function replaceWRSSemantic(article) {
72+
replaceNode(article, 'span.control', ($node, attrs, content) => (
73+
`<b ${attrs}>${content}</b>`
74+
));
6675

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-
}
76+
replaceNode(article, 'div.code-block', ($node, attrs, content) => (
77+
`<code ${attrs}>${content}</code>`
78+
));
7579
}
7680

7781
/**

0 commit comments

Comments
 (0)