mirror of
https://github.com/PR0M3TH3AN/Archivox.git
synced 2025-09-07 14:48:40 +00:00
Merge pull request #35 from PR0M3TH3AN/codex/make-markdown-docs-searchable
Enable full-text search in docs
This commit is contained in:
@@ -70,6 +70,8 @@ test('markdown files render with layout and appear in nav/search', async () => {
|
|||||||
const docs = search.docs.map(d => d.id);
|
const docs = search.docs.map(d => d.id);
|
||||||
expect(docs).toContain('index.html');
|
expect(docs).toContain('index.html');
|
||||||
expect(docs).toContain('guide/install.html');
|
expect(docs).toContain('guide/install.html');
|
||||||
|
const installDoc = search.docs.find(d => d.id === 'guide/install.html');
|
||||||
|
expect(installDoc.body).toContain('Steps');
|
||||||
|
|
||||||
fs.rmSync(tmp, { recursive: true, force: true });
|
fs.rmSync(tmp, { recursive: true, force: true });
|
||||||
});
|
});
|
||||||
|
@@ -65,7 +65,8 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
if (!doc) return;
|
if (!doc) return;
|
||||||
const a = document.createElement('a');
|
const a = document.createElement('a');
|
||||||
a.href = doc.url;
|
a.href = doc.url;
|
||||||
a.innerHTML = '<strong>' + highlight(doc.title, q) + '</strong><br><small>' + highlight(doc.headings, q) + '</small>';
|
const snippet = doc.body ? doc.body.slice(0, 160) + (doc.body.length > 160 ? '...' : '') : '';
|
||||||
|
a.innerHTML = '<strong>' + highlight(doc.title, q) + '</strong><br><small>' + highlight(snippet, q) + '</small>';
|
||||||
searchResults.appendChild(a);
|
searchResults.appendChild(a);
|
||||||
});
|
});
|
||||||
searchResults.style.display = 'block';
|
searchResults.style.display = 'block';
|
||||||
|
@@ -142,8 +142,10 @@ async function generate({ contentDir = 'content', outputDir = '_site', configPat
|
|||||||
const firstHeading = tokens.find(t => t.type === 'heading');
|
const firstHeading = tokens.find(t => t.type === 'heading');
|
||||||
const title = parsed.data.title || (firstHeading ? firstHeading.text : path.basename(rel, '.md'));
|
const title = parsed.data.title || (firstHeading ? firstHeading.text : path.basename(rel, '.md'));
|
||||||
const headings = tokens.filter(t => t.type === 'heading').map(t => t.text).join(' ');
|
const headings = tokens.filter(t => t.type === 'heading').map(t => t.text).join(' ');
|
||||||
|
const htmlBody = require('marked').parse(parsed.content || '');
|
||||||
|
const bodyText = htmlBody.replace(/<[^>]+>/g, ' ');
|
||||||
pages.push({ file: rel, data: { ...parsed.data, title } });
|
pages.push({ file: rel, data: { ...parsed.data, title } });
|
||||||
searchDocs.push({ id: rel.replace(/\.md$/, '.html'), url: '/' + rel.replace(/\.md$/, '.html'), title, headings });
|
searchDocs.push({ id: rel.replace(/\.md$/, '.html'), url: '/' + rel.replace(/\.md$/, '.html'), title, headings, body: bodyText });
|
||||||
} else {
|
} else {
|
||||||
assets.push(rel);
|
assets.push(rel);
|
||||||
}
|
}
|
||||||
@@ -158,6 +160,7 @@ async function generate({ contentDir = 'content', outputDir = '_site', configPat
|
|||||||
this.ref('id');
|
this.ref('id');
|
||||||
this.field('title');
|
this.field('title');
|
||||||
this.field('headings');
|
this.field('headings');
|
||||||
|
this.field('body');
|
||||||
searchDocs.forEach(d => this.add(d));
|
searchDocs.forEach(d => this.add(d));
|
||||||
});
|
});
|
||||||
await fs.promises.writeFile(
|
await fs.promises.writeFile(
|
||||||
|
Reference in New Issue
Block a user