diff --git a/docs/.gitignore b/docs/.gitignore index 4d27172d..0372afb9 100644 --- a/docs/.gitignore +++ b/docs/.gitignore @@ -1,3 +1,4 @@ /node_modules /dist /.routify +src/SEARCH_INDEX.json \ No newline at end of file diff --git a/docs/package.json b/docs/package.json index 961d91a1..fb581f22 100644 --- a/docs/package.json +++ b/docs/package.json @@ -1,10 +1,11 @@ { "private": true, "scripts": { - "dev": "run-p dev:*", + "dev": "yarn build:index-docs && run-p dev:*", "dev:routify": "cross-env NODE_ENV=development routify run", "dev:svite": "vite dev", "build": "run-s build:*", + "build:index-docs": "node scripts/index-docs.js", "build:routify": "routify run -b", "build:svite": "vite build" }, @@ -16,6 +17,7 @@ "clipboard-copy": "^4.0.1", "cross-env": "^7.0.3", "mdsvex": "^0.10.6", + "minisearch": "^6.2.0", "npm-run-all": "^4.1.5", "prettier": "^2.7.1", "prettier-plugin-svelte": "^2.7.0", diff --git a/docs/scripts/index-docs.js b/docs/scripts/index-docs.js new file mode 100644 index 00000000..a54532d9 --- /dev/null +++ b/docs/scripts/index-docs.js @@ -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)); diff --git a/docs/src/pages/_layout.svelte b/docs/src/pages/_layout.svelte index b506dea7..5273c072 100644 --- a/docs/src/pages/_layout.svelte +++ b/docs/src/pages/_layout.svelte @@ -1,5 +1,11 @@