Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jer3m01 committed Jan 11, 2024
1 parent ed85b03 commit 7925f3d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 66 deletions.
17 changes: 9 additions & 8 deletions apps/docs/src/components/table-of-contents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function useCurrentSection(tableOfContents: Accessor<TocItem[] | undefined>) {
}

const getTOC = cache(async (pathname: string) => {
"use server";
console.log("TOC " + pathname);
console.log("TOC " + JSON.stringify(mods));

Expand All @@ -83,17 +84,17 @@ const getTOC = cache(async (pathname: string) => {
export function TableOfContents() {
const path = useLocation();

const [toc, setTOC] = createSignal<Array<{depth: number, text: string, slug: string}>>([]);
const toc = createAsync(() => getTOC(path.pathname))// createSignal<Array<{depth: number, text: string, slug: string}>>([]);

console.log("LOADING TOC");

onMount(() => {
if (!isServer) {
const asyncTOC = createAsync(() => getTOC(path.pathname));

setTOC(asyncTOC() ?? []);
}
});
// onMount(() => {
// if (!isServer) {
// const asyncTOC = createAsync(() => getTOC(path.pathname));
//
// setTOC(asyncTOC() ?? []);
// }
// });

// const toc = createServerData$(
// async pathname => {
Expand Down
108 changes: 50 additions & 58 deletions apps/docs/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,67 +208,68 @@ interface MDXConfig {
const cache = new Map();
const headingsCache = new Map();

function rehypeCollectHeadings() {
const slugger = new Slugger();
return function (tree: any, file: any) {
const headings: any[] = [];
visit(tree, node => {
if (node.type !== "element") {
return;
}
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;
}

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;
}
visit(node, (child, __, parent) => {
if (child.type === "element" || parent == null) {
return;
}

if (child.type === "raw" && child.value.match(/^\n?<.*>\n?$/)) {
return;
}
if (child.type === "raw" && child.value.match(/^\n?<.*>\n?$/)) {
return;
}

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

node.properties = node.properties || {};
node.properties = node.properties || {};

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

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

node.properties.id = slug;
}
node.properties.id = slug;
}

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

console.log("VITE SET HEADINGS", file.path, headings);
headingsCache.set(file.path, headings);
headingsCache.set(filename, 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 @@ -293,15 +294,15 @@ export default defineConfig({
// rehypePlugins: [rehypePrettyCode],
// remarkPlugins: [remarkGfm],
// }),
vinxiMdx.withImports({})({
vinxiMdx.withImports({})(filename => ({
jsx: true,
jsxImportSource: "solid-js",
providerImportSource: "solid-mdx",
rehypePlugins: [
[rehypeRaw, { passThrough: nodeTypes }],
rehypePrettyCode,
rehypeSlug,
rehypeCollectHeadings,
rehypeCollectHeadings(filename),
],
remarkPlugins: [
remarkGfm,
Expand Down Expand Up @@ -331,25 +332,16 @@ export default defineConfig({
},
],
],
}),
})),
{ enforce: "pre" },
{
name: "mdx-meta-load",
async transform(code: any, id: any) {
if (id.endsWith(".mdx") || id.endsWith(".md")) {
console.log("VITE mdx-meta-load " + id);
}
},
},
{
name: "mdx-meta",
async transform(code: any, id: any) {
if (id.endsWith(".mdx?meta") || id.endsWith(".md?meta")) {
console.log("VITE " + id);

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

console.log("VITE CACHE", headingsCache);
console.log("VITE LOOKUP " + id);

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

0 comments on commit 7925f3d

Please sign in to comment.