-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
673130f
commit 620cc2e
Showing
10 changed files
with
136 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import { generateEditedDates } from "build-scripts" | ||
import path from "path" | ||
import { generateTags } from "tags" | ||
|
||
async function main() { | ||
await generateEditedDates() | ||
await generateTags(path.resolve("..", "..", "packages", "tags")) | ||
} | ||
|
||
void main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import { generateEditedDates, generateSidebar } from "build-scripts" | ||
import { generateTags } from "tags" | ||
import { main as generateSlugChanges } from "./generate-slug-changes.mjs" | ||
import { main as generateFilesMap } from "./generate-files-map.mjs" | ||
import { sidebar } from "../sidebar.mjs" | ||
import path from "path" | ||
|
||
async function main() { | ||
await generateSidebar(sidebar) | ||
await generateSlugChanges() | ||
await generateFilesMap() | ||
await generateEditedDates() | ||
await generateTags(path.resolve("..", "..", "packages", "tags")) | ||
} | ||
|
||
void main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./utils/index.js" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,3 @@ | ||
import { statSync } from "fs" | ||
import { mkdir, readdir, rm, writeFile } from "fs/promises" | ||
import path from "path" | ||
import type { Tags } from "types" | ||
import { findPageTitle, getFrontMatterSync } from "utils" | ||
import { generateTags } from "../utils/generate-tags.js" | ||
|
||
type ConfigItem = { | ||
path: string | ||
contentPaths: string[] | ||
} | ||
|
||
const config: ConfigItem[] = [ | ||
{ | ||
path: path.resolve("..", "..", "apps", "book"), | ||
contentPaths: ["app"], | ||
}, | ||
{ | ||
path: path.resolve("..", "..", "apps", "resources"), | ||
contentPaths: ["app", "references"], | ||
}, | ||
{ | ||
path: path.resolve("..", "..", "apps", "ui"), | ||
contentPaths: [path.join("src", "content", "docs")], | ||
}, | ||
{ | ||
path: path.resolve("..", "..", "apps", "user-guide"), | ||
contentPaths: ["app"], | ||
}, | ||
] | ||
|
||
function normalizePageTitle(title: string): string { | ||
// remove variables from title | ||
return title.replaceAll(/\$\{.+\}/g, "").trim() | ||
} | ||
|
||
function tagNameToFileName(tagName: string): string { | ||
return `${tagName.toLowerCase().replaceAll(" ", "-")}.ts` | ||
} | ||
|
||
function tagNameToVarName(tagName: string): string { | ||
return tagName | ||
.toLowerCase() | ||
.replaceAll(/\s([a-zA-Z\d])/g, (captured) => captured.toUpperCase().trim()) | ||
} | ||
|
||
async function main() { | ||
const tags: Tags = {} | ||
async function getTags(item: ConfigItem) { | ||
async function scanDirectory(dirPath: string) { | ||
const files = await readdir(dirPath) | ||
|
||
for (const file of files) { | ||
const fullPath = path.join(dirPath, file) | ||
if (!file.endsWith(".mdx") || file.startsWith("_")) { | ||
if (statSync(fullPath).isDirectory()) { | ||
await scanDirectory(fullPath) | ||
} | ||
continue | ||
} | ||
|
||
const frontmatter = getFrontMatterSync(fullPath) | ||
const fileBasename = path.basename(file) | ||
|
||
frontmatter.tags?.forEach((tag) => { | ||
if (!Object.hasOwn(tags, tag)) { | ||
tags[tag] = [] | ||
} | ||
|
||
tags[tag].push({ | ||
title: normalizePageTitle( | ||
frontmatter.sidebar_label || findPageTitle(fullPath) || "" | ||
), | ||
path: | ||
frontmatter.slug || | ||
fullPath.replace(item.path, "").replace(`/${fileBasename}`, ""), | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
for (const contentPath of item.contentPaths) { | ||
const basePath = path.join(item.path, contentPath) | ||
|
||
await scanDirectory(basePath) | ||
} | ||
} | ||
|
||
await Promise.all( | ||
config.map(async (item) => { | ||
await getTags(item) | ||
}) | ||
) | ||
|
||
const tagsDir = path.join("src", "tags") | ||
// clear existing tags | ||
await rm(tagsDir, { | ||
recursive: true, | ||
force: true, | ||
}) | ||
await mkdir(tagsDir) | ||
// write tags | ||
const files: string[] = [] | ||
await Promise.all( | ||
Object.keys(tags).map(async (tagName) => { | ||
const fileName = tagNameToFileName(tagName) | ||
const varName = tagNameToVarName(tagName) | ||
|
||
const content = `export const ${varName} = ${JSON.stringify(tags[tagName], null, 2)}` | ||
|
||
await writeFile(path.join(tagsDir, fileName), content) | ||
files.push(fileName.replace(/\.ts$/, ".js")) | ||
}) | ||
) | ||
|
||
// write index.ts | ||
const indexContent = files.map((file) => `export * from "./${file}"\n`) | ||
await writeFile(path.join(tagsDir, "index.ts"), indexContent) | ||
} | ||
|
||
void main() | ||
void generateTags() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { statSync } from "fs" | ||
import { mkdir, readdir, rm, writeFile } from "fs/promises" | ||
import path from "path" | ||
import type { Tags } from "types" | ||
import { findPageTitle, getFrontMatterSync } from "utils" | ||
|
||
type ConfigItem = { | ||
path: string | ||
contentPaths: string[] | ||
} | ||
|
||
const config: ConfigItem[] = [ | ||
{ | ||
path: path.resolve("..", "..", "apps", "book"), | ||
contentPaths: ["app"], | ||
}, | ||
{ | ||
path: path.resolve("..", "..", "apps", "resources"), | ||
contentPaths: ["app", "references"], | ||
}, | ||
{ | ||
path: path.resolve("..", "..", "apps", "ui"), | ||
contentPaths: [path.join("src", "content", "docs")], | ||
}, | ||
{ | ||
path: path.resolve("..", "..", "apps", "user-guide"), | ||
contentPaths: ["app"], | ||
}, | ||
] | ||
|
||
function normalizePageTitle(title: string): string { | ||
// remove variables from title | ||
return title.replaceAll(/\$\{.+\}/g, "").trim() | ||
} | ||
|
||
function tagNameToFileName(tagName: string): string { | ||
return `${tagName.toLowerCase().replaceAll(" ", "-")}.ts` | ||
} | ||
|
||
function tagNameToVarName(tagName: string): string { | ||
return tagName | ||
.toLowerCase() | ||
.replaceAll(/\s([a-zA-Z\d])/g, (captured) => captured.toUpperCase().trim()) | ||
} | ||
|
||
export async function generateTags(basePath?: string) { | ||
basePath = basePath || path.resolve() | ||
const tags: Tags = {} | ||
async function getTags(item: ConfigItem) { | ||
async function scanDirectory(dirPath: string) { | ||
const files = await readdir(dirPath) | ||
|
||
for (const file of files) { | ||
const fullPath = path.join(dirPath, file) | ||
if (!file.endsWith(".mdx") || file.startsWith("_")) { | ||
if (statSync(fullPath).isDirectory()) { | ||
await scanDirectory(fullPath) | ||
} | ||
continue | ||
} | ||
|
||
const frontmatter = getFrontMatterSync(fullPath) | ||
const fileBasename = path.basename(file) | ||
|
||
frontmatter.tags?.forEach((tag) => { | ||
if (!Object.hasOwn(tags, tag)) { | ||
tags[tag] = [] | ||
} | ||
|
||
tags[tag].push({ | ||
title: normalizePageTitle( | ||
frontmatter.sidebar_label || findPageTitle(fullPath) || "" | ||
), | ||
path: | ||
frontmatter.slug || | ||
fullPath.replace(item.path, "").replace(`/${fileBasename}`, ""), | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
for (const contentPath of item.contentPaths) { | ||
const basePath = path.join(item.path, contentPath) | ||
|
||
await scanDirectory(basePath) | ||
} | ||
} | ||
|
||
await Promise.all( | ||
config.map(async (item) => { | ||
await getTags(item) | ||
}) | ||
) | ||
|
||
const tagsDir = path.join(basePath, "src", "tags") | ||
// clear existing tags | ||
await rm(tagsDir, { | ||
recursive: true, | ||
force: true, | ||
}) | ||
await mkdir(tagsDir) | ||
// write tags | ||
const files: string[] = [] | ||
await Promise.all( | ||
Object.keys(tags).map(async (tagName) => { | ||
const fileName = tagNameToFileName(tagName) | ||
const varName = tagNameToVarName(tagName) | ||
|
||
const content = `export const ${varName} = ${JSON.stringify(tags[tagName], null, 2)}` | ||
|
||
await writeFile(path.join(tagsDir, fileName), content) | ||
files.push(fileName.replace(/\.ts$/, ".js")) | ||
}) | ||
) | ||
|
||
// write index.ts | ||
const indexContent = files.map((file) => `export * from "./${file}"\n`) | ||
await writeFile(path.join(tagsDir, "index.ts"), indexContent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./generate-tags.js" | ||
export * from "./tags.js" |
2 changes: 1 addition & 1 deletion
2
www/packages/tags/src/utils.ts → www/packages/tags/src/utils/tags.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters