Skip to content

Commit

Permalink
fix(docs): TOC
Browse files Browse the repository at this point in the history
  • Loading branch information
jer3m01 committed Jan 17, 2024
1 parent 7925f3d commit 99fbdda
Showing 1 changed file with 47 additions and 54 deletions.
101 changes: 47 additions & 54 deletions apps/docs/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import rehypeSlug from "rehype-slug";
import remarkGfm from "remark-gfm";
import remarkShikiTwoslash from "remark-shiki-twoslash";
import { visit } from "unist-util-visit";
import { Plugin } from "vite";

const { default: vinxiMdx } = pkg;

Expand Down Expand Up @@ -205,71 +204,68 @@ interface MDXConfig {
// ];
//}

const cache = new Map();
const headingsCache = new Map();

function rehypeCollectHeadings(filename: string) {
return () => {
const slugger = new Slugger();
return function (tree: any, file: any) {
const headings: any[] = [];
visit(tree, node => {
if (node.type !== "element") {
return;
}
function rehypeCollectHeadings() {
const slugger = new Slugger();
return function (tree: any, file: any) {
const headings: any[] = [];
visit(tree, node => {
if (node.type !== "element") {
return;
}

const { tagName } = node;
const { tagName } = node;

if (tagName[0] !== "h") {
return;
}
if (tagName[0] !== "h") {
return;
}

const [_, level] = tagName.match(/h([0-6])/) ?? [];
const [_, level] = tagName.match(/h([0-6])/) ?? [];

if (!level) {
return;
}
if (!level) {
return;
}

const depth = Number.parseInt(level);
const depth = Number.parseInt(level);

let text = "";
let text = "";

visit(node, (child, __, parent) => {
if (child.type === "element" || parent == null) {
return;
}

if (child.type === "raw" && child.value.match(/^\n?<.*>\n?$/)) {
return;
}
visit(node, (child, __, parent) => {
if (child.type === "element" || parent == null) {
return;
}

if (new Set(["text", "raw", "mdxTextExpression"]).has(child.type)) {
text += child.value;
}
});
if (child.type === "raw" && child.value.match(/^\n?<.*>\n?$/)) {
return;
}

node.properties = node.properties || {};
if (new Set(["text", "raw", "mdxTextExpression"]).has(child.type)) {
text += child.value;
}
});

if (typeof node.properties.id !== "string") {
let slug = slugger.slug(text);
node.properties = node.properties || {};

if (slug.endsWith("-")) {
slug = slug.slice(0, -1);
}
if (typeof node.properties.id !== "string") {
let slug = slugger.slug(text);

node.properties.id = slug;
if (slug.endsWith("-")) {
slug = slug.slice(0, -1);
}

headings.push({ depth, slug: node.properties.id, text });
});
node.properties.id = slug;
}

headings.push({ depth, slug: node.properties.id, text });
});

headingsCache.set(filename, headings);
headingsCache.set(file.path, headings);

tree.children.unshift(
// @ts-ignoret
jsToTreeNode(`export function getHeadings() { return ${JSON.stringify(headings)} }`),
);
};
tree.children.unshift(
// @ts-ignoret
jsToTreeNode(`export function getHeadings() { return ${JSON.stringify(headings)} }`),
);
};
}

Expand All @@ -294,15 +290,15 @@ export default defineConfig({
// rehypePlugins: [rehypePrettyCode],
// remarkPlugins: [remarkGfm],
// }),
vinxiMdx.withImports({})(filename => ({
vinxiMdx.withImports({})({
jsx: true,
jsxImportSource: "solid-js",
providerImportSource: "solid-mdx",
rehypePlugins: [
[rehypeRaw, { passThrough: nodeTypes }],
rehypePrettyCode,
rehypeSlug,
rehypeCollectHeadings(filename),
rehypeCollectHeadings,
],
remarkPlugins: [
remarkGfm,
Expand Down Expand Up @@ -332,17 +328,14 @@ export default defineConfig({
},
],
],
})),
}),
{ enforce: "pre" },
{
name: "mdx-meta",
async transform(code: any, id: any) {
if (id.endsWith(".mdx?meta") || id.endsWith(".md?meta")) {

id = id.replace(/\?meta$/, "");

console.log("VITE LOOKUP " + id);

// eslint-disable-next-line no-inner-declarations
function getCode() {
return `
Expand Down

0 comments on commit 99fbdda

Please sign in to comment.