Skip to content

Commit d60dbb8

Browse files
authored
Merge pull request #4213 from JetBrains/ktl-1490-writerside
KTL-1490 Use WriterSide for the documentation
2 parents f2ebf70 + 59821b4 commit d60dbb8

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
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: 42 additions & 2 deletions
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,45 @@ 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+
* @param {string} selector
55+
* @param {($node: import('cheerio').Cheerio<Element>, attrs: string, content: string) => string} cb
56+
*/
57+
function replaceNode(article, selector, cb) {
58+
const listStrong = article.find(selector);
59+
60+
for (let i = 0, length = listStrong.length; i < length; i++) {
61+
const $node = listStrong.eq(i);
62+
const newNode = cb($node, cloneAttrsString(listStrong[i]), $node.html());
63+
$node.replaceWith(newNode);
64+
}
65+
}
66+
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+
));
75+
76+
replaceNode(article, 'div.code-block', ($node, attrs, content) => (
77+
`<code ${attrs}>${content}</code>`
78+
));
79+
}
80+
4281
/**
4382
* @param {import('cheerio').Cheerio} article
4483
* @returns {void}
@@ -158,10 +197,11 @@ async function docs($, url, data) {
158197

159198
/** @type {import('cheerio').Cheerio} */
160199
const $article = $('article.article');
161-
const pageUrl = new URL($('link[rel="canonical"]').attr('href'));
200+
const pageUrl = new URL($('meta[property="og:url"]').attr('content'));
162201

163202
dropUiElements($article);
164203
dropIrrelevantSections($article);
204+
replaceWRSSemantic($article);
165205
replaceMedia($article, pageUrl.toString());
166206

167207
const breadcrumbs = getBreadcrumbs($body);

0 commit comments

Comments
 (0)