fix: embed setup -- now embedded content should render properly based on the remark setup in the astro config.

This commit is contained in:
manunamz
2024-09-03 14:47:14 -04:00
parent 06ebc36941
commit 7ece9da7fc
2 changed files with 75 additions and 42 deletions
+56 -25
View File
@@ -1,13 +1,69 @@
import type { Processor } from 'unified';
import path from 'path';
import fs from 'fs';
import fg from 'fast-glob';
import matter from 'gray-matter';
import { selectAll } from 'unist-util-select';
import * as wikirefs from 'wikirefs';
import { unified } from 'unified';
import remarkParse from 'remark-parse';
const CONTENT_GLOB: string = './src/content/**/*.md';
let globalProcessor: Processor;
let cycleStack: string[] = [];
export function createResolveEmbedContent(remarkPlugins: any[]) {
console.log("createResolveEmbedContent", remarkPlugins);
if (!globalProcessor) {
globalProcessor = unified().use(remarkParse);
// apply same remark plugins as in astro config
remarkPlugins.forEach(plugin => {
if (Array.isArray(plugin)) {
globalProcessor.use(plugin[0], plugin[1]);
} else {
globalProcessor.use(plugin);
}
});
// note: we're not using remarkRehype or rehypeStringify here
}
return function resolveEmbedContentInternal(filename: string): any {
// markdown-only
if (wikirefs.isMedia(filename)) { return; }
// cycle detection
if (cycleStack.length === 0) {
cycleStack = [];
} else if (cycleStack.includes(filename)) {
// reset stack before leaving
cycleStack = [];
return 'cycle detected';
}
cycleStack.push(filename);
const docPaths: string[] = fg.sync(CONTENT_GLOB);
const docPath: string | undefined = docPaths.find((p) => {
return path.basename(p, '.md') === filename;
});
let content: string = '';
if (docPath === undefined) {
content = 'Error: Content not found for ' + "'" + filename + "'";
} else {
const text: string | undefined = fs.readFileSync(docPath, { encoding: 'utf8', flag: 'r' });
content = matter(text).content;
}
let mdastContent: any;
if (content.length === 0) {
mdastContent = '';
} else {
mdastContent = globalProcessor.parse(content);
}
// reset stack before leaving
cycleStack = [];
return mdastContent;
};
}
export function resolveHtmlHref(fname: string): string | undefined {
// #todo revisit: https://docs.astro.build/en/reference/api-reference/#importmeta
// 'import.meta.glob' docs:
@@ -66,31 +122,6 @@ export function resolveHtmlText(fname: string): string | undefined {
}
}
export function resolveEmbedContent(filename: string): any {
// markdown-only
if (wikirefs.isMedia(filename)) { return; }
const docPaths: string[] = fg.sync(CONTENT_GLOB);
const docPath: string | undefined = docPaths.find((p) => {
return path.basename(p, '.md') === filename;
});
let content: string = '';
if (docPath === undefined) {
content = 'Error: Content not found for ' + "'" + filename + "'";
} else {
const text: string | undefined = fs.readFileSync(docPath, { encoding: 'utf8', flag: 'r' });
content = matter(text).content;
}
// return mdast node
return {
type: 'text',
// todo: render
value: content,
};
}
// forerfs
// todo: don't include index documents when calculating metadata -- and skip index docs too
export function generateForeRefsRemarkPlugin() {
return function (tree: any, file: any) {
// from: https://observablehq.com/@tmcw/extracting-links-from-markdown-using-remark-and-unist-util