2023-05-17 13:13:10 +00:00
|
|
|
import type { MarkdownHeading } from '@astrojs/markdown-remark';
|
2023-05-24 20:52:22 +00:00
|
|
|
import Markdoc, { type RenderableTreeNode } from '@markdoc/markdoc';
|
2023-05-17 13:13:10 +00:00
|
|
|
import type { ContentEntryModule } from 'astro';
|
2023-05-24 20:52:22 +00:00
|
|
|
import type { AstroMarkdocConfig } from './config.js';
|
2023-05-24 20:54:43 +00:00
|
|
|
import { setupHeadingConfig } from './heading-ids.js';
|
2023-05-17 13:13:10 +00:00
|
|
|
|
2023-05-19 18:12:45 +00:00
|
|
|
/** Used to call `Markdoc.transform()` and `Markdoc.Ast` in runtime modules */
|
2023-05-17 13:13:10 +00:00
|
|
|
export { default as Markdoc } from '@markdoc/markdoc';
|
|
|
|
|
2023-05-19 18:12:45 +00:00
|
|
|
/**
|
|
|
|
* Merge user config with default config and set up context (ex. heading ID slugger)
|
2023-05-24 20:52:22 +00:00
|
|
|
* Called on each file's individual transform.
|
|
|
|
* TODO: virtual module to merge configs per-build instead of per-file?
|
2023-05-19 18:12:45 +00:00
|
|
|
*/
|
2023-05-25 15:35:07 +00:00
|
|
|
export async function setupConfig(
|
2023-05-24 20:52:22 +00:00
|
|
|
userConfig: AstroMarkdocConfig,
|
2023-05-25 15:35:07 +00:00
|
|
|
entry: ContentEntryModule
|
|
|
|
): Promise<Omit<AstroMarkdocConfig, 'extends'>> {
|
2023-05-24 20:52:22 +00:00
|
|
|
let defaultConfig: AstroMarkdocConfig = {
|
2023-05-19 18:12:45 +00:00
|
|
|
...setupHeadingConfig(),
|
|
|
|
variables: { entry },
|
|
|
|
};
|
2023-05-24 20:52:22 +00:00
|
|
|
|
|
|
|
if (userConfig.extends) {
|
2023-05-25 15:35:07 +00:00
|
|
|
for (let extension of userConfig.extends) {
|
2023-05-24 20:52:22 +00:00
|
|
|
if (extension instanceof Promise) {
|
2023-05-25 15:35:07 +00:00
|
|
|
extension = await extension;
|
2023-05-24 20:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defaultConfig = mergeConfig(defaultConfig, extension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 18:12:45 +00:00
|
|
|
return mergeConfig(defaultConfig, userConfig);
|
|
|
|
}
|
|
|
|
|
2023-05-25 15:35:07 +00:00
|
|
|
/** Used for synchronous `getHeadings()` function */
|
|
|
|
export function setupConfigSync(
|
|
|
|
userConfig: AstroMarkdocConfig,
|
|
|
|
entry: ContentEntryModule
|
|
|
|
): Omit<AstroMarkdocConfig, 'extends'> {
|
|
|
|
let defaultConfig: AstroMarkdocConfig = {
|
|
|
|
...setupHeadingConfig(),
|
|
|
|
variables: { entry },
|
|
|
|
};
|
|
|
|
|
|
|
|
return mergeConfig(defaultConfig, userConfig);
|
|
|
|
}
|
|
|
|
|
2023-05-19 18:12:45 +00:00
|
|
|
/** Merge function from `@markdoc/markdoc` internals */
|
2023-05-24 20:52:22 +00:00
|
|
|
function mergeConfig(configA: AstroMarkdocConfig, configB: AstroMarkdocConfig): AstroMarkdocConfig {
|
2023-05-17 13:13:10 +00:00
|
|
|
return {
|
2023-05-19 18:12:45 +00:00
|
|
|
...configA,
|
|
|
|
...configB,
|
2023-05-24 20:52:22 +00:00
|
|
|
ctx: {
|
|
|
|
...configA.ctx,
|
|
|
|
...configB.ctx,
|
|
|
|
},
|
2023-05-19 18:12:45 +00:00
|
|
|
tags: {
|
|
|
|
...configA.tags,
|
|
|
|
...configB.tags,
|
2023-05-17 13:13:10 +00:00
|
|
|
},
|
|
|
|
nodes: {
|
2023-05-19 18:12:45 +00:00
|
|
|
...configA.nodes,
|
|
|
|
...configB.nodes,
|
|
|
|
},
|
|
|
|
functions: {
|
|
|
|
...configA.functions,
|
|
|
|
...configB.functions,
|
|
|
|
},
|
|
|
|
variables: {
|
|
|
|
...configA.variables,
|
|
|
|
...configB.variables,
|
2023-05-17 13:13:10 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get text content as a string from a Markdoc transform AST
|
|
|
|
*/
|
|
|
|
export function getTextContent(childNodes: RenderableTreeNode[]): string {
|
|
|
|
let text = '';
|
|
|
|
for (const node of childNodes) {
|
|
|
|
if (typeof node === 'string' || typeof node === 'number') {
|
|
|
|
text += node;
|
|
|
|
} else if (typeof node === 'object' && Markdoc.Tag.isTag(node)) {
|
|
|
|
text += getTextContent(node.children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
const headingLevels = [1, 2, 3, 4, 5, 6] as const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collect headings from Markdoc transform AST
|
|
|
|
* for `headings` result on `render()` return value
|
|
|
|
*/
|
|
|
|
export function collectHeadings(children: RenderableTreeNode[]): MarkdownHeading[] {
|
|
|
|
let collectedHeadings: MarkdownHeading[] = [];
|
|
|
|
for (const node of children) {
|
|
|
|
if (typeof node !== 'object' || !Markdoc.Tag.isTag(node)) continue;
|
|
|
|
|
|
|
|
if (node.attributes.__collectHeading === true && typeof node.attributes.level === 'number') {
|
|
|
|
collectedHeadings.push({
|
|
|
|
slug: node.attributes.id,
|
|
|
|
depth: node.attributes.level,
|
|
|
|
text: getTextContent(node.children),
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const level of headingLevels) {
|
|
|
|
if (node.name === 'h' + level) {
|
|
|
|
collectedHeadings.push({
|
|
|
|
slug: node.attributes.id,
|
|
|
|
depth: level,
|
|
|
|
text: getTextContent(node.children),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
collectedHeadings.concat(collectHeadings(node.children));
|
|
|
|
}
|
|
|
|
return collectedHeadings;
|
|
|
|
}
|