mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-14 18:01:06 +00:00
docs: implement rudimentary full-text search (#1849)
This commit is contained in:
parent
6da4572c26
commit
e5e13a9e78
5 changed files with 96 additions and 5 deletions
43
docs/scripts/index-docs.js
Normal file
43
docs/scripts/index-docs.js
Normal file
|
@ -0,0 +1,43 @@
|
|||
// @ts-check
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const { slug } = require("github-slugger");
|
||||
|
||||
const COMPONENTS_PATH = "./src/pages/components";
|
||||
const SEARCH_INDEX_PATH = "./src/SEARCH_INDEX.json";
|
||||
const H2_DELMIMITER = "## ";
|
||||
|
||||
const files = fs.readdirSync(COMPONENTS_PATH);
|
||||
|
||||
/**
|
||||
* @typedef {Object} Document
|
||||
* @property {string} id
|
||||
* @property {string} text
|
||||
* @property {string} description
|
||||
* @property {string} href
|
||||
*/
|
||||
|
||||
/** @type {Document[]} */
|
||||
const documents = [];
|
||||
|
||||
for (const file of files) {
|
||||
const [component_name] = file.split(".");
|
||||
const file_path = path.join(COMPONENTS_PATH, file);
|
||||
const file_content = fs.readFileSync(file_path, "utf8");
|
||||
|
||||
file_content.split("\n").forEach((line) => {
|
||||
if (line.startsWith(H2_DELMIMITER)) {
|
||||
const [, h2] = line.split(H2_DELMIMITER);
|
||||
const hash = slug(h2);
|
||||
|
||||
documents.push({
|
||||
id: component_name + hash,
|
||||
text: component_name,
|
||||
description: h2,
|
||||
href: `/components/${component_name}#${hash}`,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fs.writeFileSync(SEARCH_INDEX_PATH, JSON.stringify(documents, null, 2));
|
Loading…
Add table
Add a link
Reference in a new issue