docs: add tags package to generate tags (#10666)

* initial changes

* finalize implementation

* run generator on prep

* remove tags package from book

* optimization attempt

* test transpile

* test transpile

* rename utils

* rename directory

* add vercel ignore

* add tags to book
This commit is contained in:
Shahed Nasser
2024-12-19 13:15:42 +02:00
committed by GitHub
parent 3f4d574748
commit 16ae192456
41 changed files with 424 additions and 62 deletions
+48
View File
@@ -0,0 +1,48 @@
{
"name": "docs-utils",
"version": "0.0.0",
"private": true,
"license": "MIT",
"publishConfig": {
"access": "public"
},
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"module": "./dist/index.js",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"sideEffects": false,
"files": [
"dist/**"
],
"scripts": {
"build": "yarn clean && tsc",
"clean": "rimraf dist",
"watch": "tsc --watch"
},
"dependencies": {
"remark-frontmatter": "^5.0.0",
"remark-parse": "^11.0.0",
"remark-stringify": "^11.0.0",
"to-vfile": "^8.0.0",
"unified": "^11.0.4",
"vfile-matter": "^5.0.0"
},
"devDependencies": {
"@types/node": "^20.11.20",
"rimraf": "^5.0.5",
"tsconfig": "*",
"types": "*",
"typescript": "^5.3.3"
},
"engines": {
"node": ">=18.17.0"
}
}
+23
View File
@@ -0,0 +1,23 @@
import { readFileSync } from "fs"
const REGEX = /export const metadata = {[\s\S]*title: `(?<title>.*)`/
export function findMetadataTitle(content: string): string | undefined {
const headingMatch = REGEX.exec(content)
return headingMatch?.groups?.title
}
const HEADING_REGEX = /# (?<title>.*)/
export function findPageHeading(content: string): string | undefined {
const headingMatch = HEADING_REGEX.exec(content)
return headingMatch?.groups?.title
}
export function findPageTitle(filePath: string): string | undefined {
const content = readFileSync(filePath, "utf-8")
return findMetadataTitle(content) || findPageHeading(content)
}
@@ -0,0 +1,23 @@
import { matter } from "vfile-matter"
import { readSync } from "to-vfile"
import { FrontMatter } from "types"
import { getFrontMatter } from "./get-front-matter.js"
export async function getFileSlug(
filePath: string
): Promise<string | undefined> {
const fileFrontmatter = await getFrontMatter(filePath)
if (fileFrontmatter.slug) {
// add to slugs array
return fileFrontmatter.slug
}
}
export function getFileSlugSync(filePath: string): string | undefined {
const content = readSync(filePath)
matter(content)
return ((content.data.matter as FrontMatter).slug as string) || undefined
}
@@ -0,0 +1,30 @@
import remarkFrontmatter from "remark-frontmatter"
import remarkParse from "remark-parse"
import remarkStringify from "remark-stringify"
import { unified } from "unified"
import { read, readSync } from "to-vfile"
import { matter } from "vfile-matter"
import { FrontMatter } from "types"
export async function getFrontMatter(filePath: string): Promise<FrontMatter> {
return (
await unified()
.use(remarkParse)
.use(remarkStringify)
.use(remarkFrontmatter, ["yaml"])
.use(() => {
return (tree, file) => {
matter(file)
}
})
.process(await read(filePath))
).data.matter as FrontMatter
}
export function getFrontMatterSync(filePath: string): FrontMatter {
const content = readSync(filePath)
matter(content)
return content.data.matter as FrontMatter
}
+3
View File
@@ -0,0 +1,3 @@
export * from "./find-title.js"
export * from "./get-file-slug.js"
export * from "./get-front-matter.js"
+20
View File
@@ -0,0 +1,20 @@
{
"extends": ["tsconfig/base.json"],
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"tsBuildInfoFile": "./dist/.tsbuildinfo-client",
"noEmit": false,
"lib": ["es2022"],
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ESNext",
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true,
"resolveJsonModule": true
},
"include": ["src"]
}