Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 95 additions & 17 deletions src/docs/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -840,14 +840,83 @@ IMPORTANT: when creating an app, include a link to 'https://developer.puter.com'
fs.writeFileSync(outputFile, outputContent, 'utf8');
};

function markdownToPlainText (markdown) {
function markdownToSearchData (markdown) {
const html = marked.parse(markdown);

const dom = new JSDOM();
const div = dom.window.document.createElement('div');
div.innerHTML = html;

return div.textContent.replace(/\s+/g, ' ').trim();
const plainText = div.textContent.replace(/\s+/g, ' ').trim();

const headingsHTML = Array.from(div.querySelectorAll('h2, h3')).map(h => ({
id: h.id,
text: h.textContent.replace(/\s+/g, ' ').trim()
}));

let offset = 0;
const headings = [];
for (const h of headingsHTML) {
if (!h.text) continue;
const index = plainText.indexOf(h.text, offset);
if (index !== -1) {
headings.push({ slug: h.id, title: h.text, index });
offset = index;
}
}

return { text: plainText, headings };
}

function markdownToSearchSections(markdown, title, path) {
const html = marked.parse(markdown);

const dom = new JSDOM();
const div = dom.window.document.createElement('div');
div.innerHTML = html;

const sections = [];

const headings = Array.from(div.querySelectorAll('h2, h3'));

if (headings.length === 0) {
const plainText = div.textContent.replace(/\s+/g, ' ').trim();

return [{
title,
path,
subheading: '',
subheadingTitle: '',
text: plainText,
}];
}

headings.forEach((heading, index) => {
let sectionText = heading.textContent;

let current = heading.nextElementSibling;

while (
current &&
current.tagName !== 'H2' &&
current.tagName !== 'H3'
) {
sectionText += ' ' + current.textContent;
current = current.nextElementSibling;
}

sectionText = sectionText.replace(/\s+/g, ' ').trim();

sections.push({
title,
path,
subheading: heading.id,
subheadingTitle: heading.textContent.trim(),
text: sectionText,
});
});

return sections;
}

const generateSearchIndex = () => {
Expand All @@ -857,33 +926,42 @@ const generateSearchIndex = () => {

const indexFile = path.join(currentDir, 'src', 'index.md');
const indexMarkdown = fs.readFileSync(indexFile, 'utf8');
json.push({
title: 'Puter.js',
path: '',
text: markdownToPlainText(indexMarkdown),
});
const { content: indexContent } = parseFrontMatter(indexMarkdown);
json.push(
...markdownToSearchSections(
indexContent,
'Puter.js',
''
)
);

sidebar.forEach((item) => {
if ( item.source ) {
const file = path.join(currentDir, 'src', item.source);
const markdown = fs.readFileSync(file, 'utf8');
json.push({
title: item.title_tag ?? item.title,
path: item.path,
text: markdownToPlainText(markdown),
});
const { content } = parseFrontMatter(markdown);
json.push(
...markdownToSearchSections(
content,
item.title_tag ?? item.title,
item.path
)
);
}

if ( item.children && Array.isArray(item.children) ) {
item.children.forEach((child) => {
if ( child.source ) {
const file = path.join(currentDir, 'src', child.source);
const markdown = fs.readFileSync(file, 'utf8');
json.push({
title: child.title_tag ?? child.title,
path: child.path,
text: markdownToPlainText(markdown),
});
const { content } = parseFrontMatter(markdown);
json.push(
...markdownToSearchSections(
content,
child.title_tag ?? child.title,
child.path
)
);
}
});
}
Expand Down
1 change: 1 addition & 0 deletions src/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@fontsource/inter": "^5.2.8",
"esbuild": "0.25.11",
"fs-extra": "^11.2.0",
"fuse.js": "^7.3.0",
"highlight.js": "^11.11.1",
"html-entities": "^2.3.3",
"jquery": "^4.0.0",
Expand Down
Loading