chore: migrate to biomejs

This commit is contained in:
BennyKok
2024-01-20 20:09:28 +08:00
parent c98a16a2dd
commit e400966117
20 changed files with 63 additions and 150 deletions
+10 -10
View File
@@ -27,13 +27,13 @@ function rehypeShiki() {
visit(tree, "element", (node) => {
if (node.tagName === "pre" && node.children[0]?.tagName === "code") {
let codeNode = node.children[0];
let textNode = codeNode.children[0];
const codeNode = node.children[0];
const textNode = codeNode.children[0];
node.properties.code = textNode.value;
if (node.properties.language) {
let tokens = highlighter.codeToThemedTokens(
const tokens = highlighter.codeToThemedTokens(
textNode.value,
node.properties.language
);
@@ -53,7 +53,7 @@ function rehypeShiki() {
function rehypeSlugify() {
return (tree) => {
let slugify = slugifyWithCounter();
const slugify = slugifyWithCounter();
visit(tree, "element", (node) => {
if (node.tagName === "h2" && !node.properties.id) {
node.properties.id = slugify(toString(node));
@@ -64,10 +64,10 @@ function rehypeSlugify() {
function rehypeAddMDXExports(getExports) {
return (tree) => {
let exports = Object.entries(getExports(tree));
const exports = Object.entries(getExports(tree));
for (let [name, value] of exports) {
for (let node of tree.children) {
for (const [name, value] of exports) {
for (const node of tree.children) {
if (
node.type === "mdxjsEsm" &&
new RegExp(`export\\s+const\\s+${name}\\s*=`).test(node.value)
@@ -76,7 +76,7 @@ function rehypeAddMDXExports(getExports) {
}
}
let exportStr = `export const ${name} = ${value}`;
const exportStr = `export const ${name} = ${value}`;
tree.children.push({
type: "mdxjsEsm",
@@ -93,9 +93,9 @@ function rehypeAddMDXExports(getExports) {
}
function getSections(node) {
let sections = [];
const sections = [];
for (let child of node.children ?? []) {
for (const child of node.children ?? []) {
if (child.type === "element" && child.tagName === "h2") {
sections.push(`{
title: ${JSON.stringify(toString(child))},
+8 -8
View File
@@ -31,9 +31,9 @@ function extractSections() {
visit(tree, (node) => {
if (node.type === "heading" || node.type === "paragraph") {
let content = toString(excludeObjectExpressions(node));
const content = toString(excludeObjectExpressions(node));
if (node.type === "heading" && node.depth <= 2) {
let hash = node.depth === 1 ? null : slugify(content);
const hash = node.depth === 1 ? null : slugify(content);
sections.push([content, hash, []]);
} else {
sections.at(-1)?.[2].push(content);
@@ -45,7 +45,7 @@ function extractSections() {
}
export default function (nextConfig = {}) {
let cache = new Map();
const cache = new Map();
return Object.assign({}, nextConfig, {
webpack(config, options) {
@@ -53,20 +53,20 @@ export default function (nextConfig = {}) {
test: __filename,
use: [
createLoader(function () {
let appDir = path.resolve("./src/app/(docs)/docs");
const appDir = path.resolve("./src/app/(docs)/docs");
this.addContextDependency(appDir);
let files = glob.sync("**/*.mdx", { cwd: appDir });
let data = files.map((file) => {
const files = glob.sync("**/*.mdx", { cwd: appDir });
const data = files.map((file) => {
let url = `/${file.replace(/(^|\/)page\.mdx$/, "")}`;
let mdx = fs.readFileSync(path.join(appDir, file), "utf8");
const mdx = fs.readFileSync(path.join(appDir, file), "utf8");
let sections = [];
if (cache.get(file)?.[0] === mdx) {
sections = cache.get(file)[1];
} else {
let vfile = { value: mdx, sections };
const vfile = { value: mdx, sections };
processor.runSync(processor.parse(vfile), vfile);
cache.set(file, [mdx, sections]);
}