2021-10-13 21:12:50 +00:00
|
|
|
import commandLineArgs from 'command-line-args';
|
2021-09-07 15:52:58 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
import glob from 'globby';
|
|
|
|
import lunr from 'lunr';
|
2021-09-09 13:13:55 +00:00
|
|
|
import { getAllComponents } from './shared.js';
|
|
|
|
|
2021-10-13 21:12:50 +00:00
|
|
|
const { outdir } = commandLineArgs({ name: 'outdir', type: String });
|
|
|
|
const metadata = JSON.parse(fs.readFileSync(path.join(outdir, 'custom-elements.json'), 'utf8'));
|
2021-09-07 15:52:58 +00:00
|
|
|
|
2021-09-08 11:25:07 +00:00
|
|
|
console.log('Generating search index for documentation');
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
function getHeadings(markdown, maxLevel = 6) {
|
|
|
|
const headings = [];
|
|
|
|
const lines = markdown.split('\n');
|
2021-09-07 15:52:58 +00:00
|
|
|
|
2021-09-08 11:25:07 +00:00
|
|
|
lines.map(line => {
|
|
|
|
if (line.startsWith('#')) {
|
|
|
|
const level = line.match(/^(#+)/)[0].length;
|
|
|
|
const content = line.replace(/^#+/, '');
|
2021-09-07 15:52:58 +00:00
|
|
|
|
2021-09-08 11:25:07 +00:00
|
|
|
if (level <= maxLevel) {
|
|
|
|
headings.push({ level, content });
|
|
|
|
}
|
2021-09-07 15:52:58 +00:00
|
|
|
}
|
2021-09-08 11:25:07 +00:00
|
|
|
});
|
2021-09-07 15:52:58 +00:00
|
|
|
|
2021-09-08 11:25:07 +00:00
|
|
|
return headings;
|
|
|
|
}
|
2021-09-07 15:52:58 +00:00
|
|
|
|
2021-09-09 13:13:55 +00:00
|
|
|
function getMembers(markdown) {
|
|
|
|
const members = [];
|
|
|
|
const headers = markdown.match(/\[component-header:([a-z-]+)\]/g);
|
|
|
|
|
|
|
|
if (!headers) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
headers.map(header => {
|
|
|
|
const tagName = header.match(/\[component-header:([a-z-]+)\]/)[1];
|
|
|
|
const component = getAllComponents(metadata).find(component => component.tagName === tagName);
|
|
|
|
|
|
|
|
if (component) {
|
|
|
|
const fields = ['members', 'cssProperties', 'cssParts', 'slots', 'events'];
|
|
|
|
|
|
|
|
fields.map(field => {
|
|
|
|
if (component[field]) {
|
|
|
|
component[field].map(entry => {
|
|
|
|
if (entry.name) members.push(entry.name);
|
|
|
|
if (entry.description) members.push(entry.description);
|
|
|
|
if (entry.attribute) members.push(entry.attribute);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return members.join(' ');
|
|
|
|
}
|
|
|
|
|
2021-09-07 15:52:58 +00:00
|
|
|
const files = await glob('./docs/**/*.md');
|
|
|
|
const map = {};
|
|
|
|
const searchIndex = lunr(function () {
|
|
|
|
// The search index uses these field names extensively, so shortening them can save some serious bytes. The initial
|
2021-09-08 11:25:07 +00:00
|
|
|
// index file went from 468 KB => 401 KB by using single-character names!
|
2021-09-07 15:52:58 +00:00
|
|
|
this.ref('id'); // id
|
|
|
|
this.field('t', { boost: 10 }); // title
|
|
|
|
this.field('h', { boost: 5 }); // headings
|
2021-09-09 14:17:55 +00:00
|
|
|
this.field('m', { boost: 2 }); // members (props, methods, events, etc.)
|
2021-09-07 15:52:58 +00:00
|
|
|
this.field('c'); // content
|
|
|
|
|
|
|
|
files.map((file, index) => {
|
2021-09-08 11:18:23 +00:00
|
|
|
const relativePath = path.relative('./docs', file).replace(/\\/g, '/');
|
2021-09-17 20:26:57 +00:00
|
|
|
const relativePathNoExtension = relativePath.split('.').slice(0, -1).join('.');
|
2021-09-07 15:52:58 +00:00
|
|
|
const url = relativePath.replace(/\.md$/, '');
|
|
|
|
const filename = path.basename(file);
|
|
|
|
// Ignore certain directories and files
|
|
|
|
if (relativePath.startsWith('assets/') || relativePath.startsWith('dist/') || filename === '_sidebar.md') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const content = fs.readFileSync(file, 'utf8');
|
|
|
|
const allHeadings = getHeadings(content, 4);
|
2021-09-17 20:26:57 +00:00
|
|
|
const title = allHeadings.find(heading => heading.level === 1)?.content || '';
|
2021-09-07 15:52:58 +00:00
|
|
|
const headings = allHeadings
|
|
|
|
.filter(heading => heading.level > 1)
|
|
|
|
.map(heading => heading.content)
|
2021-09-17 20:26:57 +00:00
|
|
|
.concat([relativePathNoExtension])
|
|
|
|
.join(' ');
|
2021-09-09 13:13:55 +00:00
|
|
|
const members = getMembers(content);
|
2021-09-07 15:52:58 +00:00
|
|
|
|
2021-09-09 13:13:55 +00:00
|
|
|
this.add({ id: index, t: title, h: headings, m: members, c: content });
|
2021-09-07 15:52:58 +00:00
|
|
|
|
|
|
|
map[index] = { title, url };
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.writeFileSync('./docs/search.json', JSON.stringify({ searchIndex, map }), 'utf8');
|
|
|
|
})();
|