From 31f584352755f271d42517aac2e5ca8d553b1767 Mon Sep 17 00:00:00 2001 From: Alex Stephen <1325798+rambleraptor@users.noreply.github.com> Date: Mon, 23 Sep 2024 19:55:02 -0400 Subject: [PATCH] separate pages are a go (#20) --- astro.config.mjs | 18 --------------- package-lock.json | 49 ++++++++++++++++++++++++++++++++++++----- package.json | 2 +- scripts/generate.ts | 37 ++++++++++++++++--------------- scripts/src/markdown.ts | 35 +++++++++++++++++++++++++---- scripts/src/types.ts | 5 +++-- 6 files changed, 98 insertions(+), 48 deletions(-) diff --git a/astro.config.mjs b/astro.config.mjs index 3991223..cff52b9 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -28,24 +28,6 @@ export default defineConfig({ github: config.urls.repo, }, // Google Analytics tag. - head: [ - { - tag: 'script', - attrs: { - src: ``, - async: true, - }, - }, - { - tag: 'script', - content: ` - window.dataLayer = window.dataLayer || []; - function gtag(){dataLayer.push(arguments);} - gtag('js', new Date()); - gtag('config', ${config.site.ga_tag}); - `, - }, - ], sidebar: sidebar.concat(linter_sidebar) }), tailwind({ diff --git a/package-lock.json b/package-lock.json index 6f33bbc..7383714 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.0.1", "dependencies": { "@astrojs/check": "^0.9.3", - "@astrojs/starlight": "^0.26.1", + "@astrojs/starlight": "^0.28.2", "@astrojs/starlight-tailwind": "^2.0.3", "@astrojs/tailwind": "^5.1.0", "@hpcc-js/wasm": "^2.22.1", @@ -193,9 +193,9 @@ } }, "node_modules/@astrojs/starlight": { - "version": "0.26.1", - "resolved": "https://registry.npmjs.org/@astrojs/starlight/-/starlight-0.26.1.tgz", - "integrity": "sha512-0qNYWZJ+ZOdSfM7du6fGuwUhyTHtAeRIl0zYe+dF0TxDvcakplO1SYLbGGX6lEVYE3PdBne7dcJww85bXZJIIQ==", + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@astrojs/starlight/-/starlight-0.28.2.tgz", + "integrity": "sha512-Q1/Ujl2EzWX71qwqdt/0KP3wOyX6Rvyzcep/zD3hRCtw/Vi2TReh4Q2wLwz7mnbuYU9H7YvBKYknbkmjC+K/0w==", "dependencies": { "@astrojs/mdx": "^3.1.3", "@astrojs/sitemap": "^3.1.6", @@ -208,6 +208,7 @@ "hast-util-select": "^6.0.2", "hast-util-to-string": "^3.0.0", "hastscript": "^9.0.0", + "i18next": "^23.11.5", "mdast-util-directive": "^3.0.0", "mdast-util-to-markdown": "^2.1.0", "mdast-util-to-string": "^4.0.0", @@ -220,7 +221,7 @@ "vfile": "^6.0.2" }, "peerDependencies": { - "astro": "^4.8.6" + "astro": "^4.14.0" } }, "node_modules/@astrojs/starlight-tailwind": { @@ -522,6 +523,17 @@ "@babel/core": "^7.0.0-0" } }, + "node_modules/@babel/runtime": { + "version": "7.25.6", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.25.6.tgz", + "integrity": "sha512-VBj9MYyDb9tuLq7yzqjgzt6Q+IBQLrGZfdjOekyEirZPHxXWoTSGUTMrpsfi58Up73d13NfYLv8HT9vmznjzhQ==", + "dependencies": { + "regenerator-runtime": "^0.14.0" + }, + "engines": { + "node": ">=6.9.0" + } + }, "node_modules/@babel/template": { "version": "7.25.0", "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.25.0.tgz", @@ -4177,6 +4189,28 @@ "node": ">=16.17.0" } }, + "node_modules/i18next": { + "version": "23.15.1", + "resolved": "https://registry.npmjs.org/i18next/-/i18next-23.15.1.tgz", + "integrity": "sha512-wB4abZ3uK7EWodYisHl/asf8UYEhrI/vj/8aoSsrj/ZDxj4/UXPOa1KvFt1Fq5hkUHquNqwFlDprmjZ8iySgYA==", + "funding": [ + { + "type": "individual", + "url": "https://locize.com" + }, + { + "type": "individual", + "url": "https://locize.com/i18next.html" + }, + { + "type": "individual", + "url": "https://www.i18next.com/how-to/faq#i18next-is-awesome.-how-can-i-support-the-project" + } + ], + "dependencies": { + "@babel/runtime": "^7.23.2" + } + }, "node_modules/ieee754": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", @@ -6609,6 +6643,11 @@ "node": ">=8.10.0" } }, + "node_modules/regenerator-runtime": { + "version": "0.14.1", + "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz", + "integrity": "sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==" + }, "node_modules/rehype": { "version": "13.0.1", "resolved": "https://registry.npmjs.org/rehype/-/rehype-13.0.1.tgz", diff --git a/package.json b/package.json index 67bcd06..6038d60 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ }, "dependencies": { "@astrojs/check": "^0.9.3", - "@astrojs/starlight": "^0.26.1", + "@astrojs/starlight": "^0.28.2", "@astrojs/starlight-tailwind": "^2.0.3", "@astrojs/tailwind": "^5.1.0", "@hpcc-js/wasm": "^2.22.1", diff --git a/scripts/generate.ts b/scripts/generate.ts index 155212d..1908bfa 100644 --- a/scripts/generate.ts +++ b/scripts/generate.ts @@ -1,11 +1,11 @@ import * as fs from 'fs'; import * as path from 'path'; -import { load, dump } from "js-yaml"; import loadConfigFiles from './src/config'; import { buildSidebar, buildLinterSidebar, addToSidebar } from './src/sidebar'; import { type AEP, type ConsolidatedLinterRule, type GroupFile, type LinterRule, type Sidebar } from './src/types'; import { buildMarkdown, Markdown } from './src/markdown'; +import { load, dump } from "js-yaml"; const AEP_LOC = process.env.AEP_LOCATION!; const AEP_LINTER_LOC = process.env.AEP_LINTER_LOC!; @@ -31,15 +31,11 @@ async function getLinterRules(dirPath: string): Promise { } async function writePage(dirPath: string, filename: string, outputPath: string, title: string) { - let contents = new Markdown(fs.readFileSync(path.join(dirPath, filename), 'utf-8')); - let frontmatter = { + let contents = new Markdown(fs.readFileSync(path.join(dirPath, filename), 'utf-8'), {}); + contents.frontmatter = { 'title': title ?? getTitle(contents.contents) } - let final = `--- -${dump(frontmatter)} ---- -${contents.removeTitle().contents}` - fs.writeFileSync(outputPath, final, { flag: 'w' }); + fs.writeFileSync(outputPath, contents.removeTitle().build(), { flag: 'w' }); } async function writePages(dirPath: string, sidebar: Sidebar): Promise { @@ -85,10 +81,14 @@ function buildAEP(files: string[], folder: string): AEP { const yaml = load(yaml_text); - yaml.title = getTitle(md_text); + yaml.title = getTitle(md_text).replace('\n', ''); let contents = buildMarkdown(md_text, folder); + contents.frontmatter = yaml; + contents.addComponent({'names': ['Aside', 'Tabs', 'TabItem'], 'path': '@astrojs/starlight/components'}) + contents.addComponent({'names': ['Sample'], 'path': '../../components/Sample.astro'}) + // Write everything to a markdown file. return { title: yaml.title, @@ -97,19 +97,20 @@ function buildAEP(files: string[], folder: string): AEP { category: yaml.placement.category, order: yaml.placement.order, slug: yaml.slug, - contents: `--- -${dump(yaml)} ---- -import { Aside, Tabs, TabItem } from '@astrojs/starlight/components'; -import Sample from '../../components/Sample.astro'; - -${contents.contents}` + contents: contents, } } function writeMarkdown(aep: AEP) { + aep.contents.frontmatter.slug = aep.id.toString(); + const filePath = path.join("src/content/docs", `${aep.id}.mdx`) - fs.writeFileSync(filePath, aep.contents, { flag: "w" }); + fs.writeFileSync(filePath, aep.contents.build(), { flag: "w" }); + + aep.contents.frontmatter.slug = aep.slug; + + const slugPath = path.join("src/content/docs", `${aep.slug}.mdx`) + fs.writeFileSync(slugPath, aep.contents.build(), { flag: "w" }); } function writeSidebar(sideBar: any, filePath: string) { @@ -239,7 +240,7 @@ ${sections.join("\n")} } function buildRedirects(aeps: AEP[]): object { - return Object.fromEntries(aeps.map((aep) => [`/${aep.id}`, `/${aep.slug}`])); + return {}; } // Build config. diff --git a/scripts/src/markdown.ts b/scripts/src/markdown.ts index f086f9d..d5b0930 100644 --- a/scripts/src/markdown.ts +++ b/scripts/src/markdown.ts @@ -1,4 +1,5 @@ import * as path from 'path'; +import { load, dump } from "js-yaml"; const ASIDES = { 'Important': { 'title': 'Important', 'type': 'caution' }, @@ -17,13 +18,39 @@ const RULE_COLORS = { 'must not': 'font-extrabold text-red-700' } +interface Component { + names: Array + path: string +} + class Markdown { contents: string; - components: Set; + components: Array; + frontmatter: object; - constructor(contents: string) { + constructor(contents: string, frontmatter: object) { this.contents = contents; - this.components = new Set(); + this.components = []; + this.frontmatter = frontmatter; + } + + public addComponent(component: Component) { + const existingComponentIndex = this.components.findIndex(c => c.path === component.path); + if (existingComponentIndex !== -1) { + this.components[existingComponentIndex].names = [...new Set([...this.components[existingComponentIndex].names, ...component.names])]; + } else { + this.components.push(component); + } + } + + public build() : string { + return `--- +${dump(this.frontmatter)} +--- +${this.components.map((component) => `import ${component.names.length > 1 ? "{" + component.names.join(',') + "}" : component.names.join(',')} from '${component.path}';`).join('\n')} + +${this.contents} +`; } public substituteHTMLComments() { @@ -122,8 +149,8 @@ ${tab['oas']} ${tabContents(match[3].trimStart())} ` this.contents = this.contents.replace(match[0], formatted_results); - this.components.add('Aside'); } + this.addComponent({'names': ['Aside'], 'path': '@astrojs/starlight/components'}); return this; } diff --git a/scripts/src/types.ts b/scripts/src/types.ts index 6bd50ac..20f3339 100644 --- a/scripts/src/types.ts +++ b/scripts/src/types.ts @@ -1,4 +1,5 @@ import { z } from "zod"; +import type { Markdown } from "./markdown"; const Config = z.object({ hero: z.object({ @@ -40,7 +41,7 @@ interface AEP { title: string; id: string; frontmatter: object; - contents: string; + contents: Markdown; category: string; order: number; slug: string; @@ -49,7 +50,7 @@ interface AEP { interface LinterRule { title: string; aep: string; - contents: string; + contents: Markdown; filename: string; slug: string; }