summaryrefslogtreecommitdiff
path: root/packages/integrations/markdoc/src/heading-ids.ts
diff options
context:
space:
mode:
authorGravatar Ben Holmes <hey@bholmes.dev> 2023-05-24 16:52:22 -0400
committerGravatar GitHub <noreply@github.com> 2023-05-24 16:52:22 -0400
commit1efaef6be0265c68eac706623778e8ad23b33247 (patch)
tree3002f8ee7580157b7cb7df7e8741aa1c0b03448b /packages/integrations/markdoc/src/heading-ids.ts
parent7851f9258fae2f54795470253df9ce4bcd5f9cb0 (diff)
downloadastro-1efaef6be0265c68eac706623778e8ad23b33247.tar.gz
astro-1efaef6be0265c68eac706623778e8ad23b33247.tar.zst
astro-1efaef6be0265c68eac706623778e8ad23b33247.zip
Markdoc - Shiki (#7187)
* chore: remove unused util * chore: changeset * deps: shiki * wip: first stab at shiki markdoc config * feat: get shiki working! * refactor: return HTML string directly from transform * chore: move shiki to markdoc dev dep * refactor: use async cache with clear docs on why * test: transform units with Shiki config options * refactor: switch to `extends` model * refactor: nodes/ -> extensions/ * feat: raise friendly error for Promise extensions * docs: README * chore: lint * chore: dead file * chore: lowercase for fuzzy find please * fix: bad ctx spread * chore: clean up cache, add shiki imp error * chore: add shiki to optional peer deps * chore: hoist those consts * docs: more explicit "install shiki now please" Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * oops bad find and replace * chore: update changeset * nit: period haunts me --------- Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Diffstat (limited to 'packages/integrations/markdoc/src/heading-ids.ts')
-rw-r--r--packages/integrations/markdoc/src/heading-ids.ts73
1 files changed, 73 insertions, 0 deletions
diff --git a/packages/integrations/markdoc/src/heading-ids.ts b/packages/integrations/markdoc/src/heading-ids.ts
new file mode 100644
index 000000000..57b84d059
--- /dev/null
+++ b/packages/integrations/markdoc/src/heading-ids.ts
@@ -0,0 +1,73 @@
+import Markdoc, { type ConfigType, type RenderableTreeNode, type Schema } from '@markdoc/markdoc';
+import Slugger from 'github-slugger';
+import { getTextContent } from './runtime.js';
+import type { AstroMarkdocConfig } from './config.js';
+import { MarkdocError } from './utils.js';
+
+function getSlug(
+ attributes: Record<string, any>,
+ children: RenderableTreeNode[],
+ headingSlugger: Slugger
+): string {
+ if (attributes.id && typeof attributes.id === 'string') {
+ return attributes.id;
+ }
+ const textContent = attributes.content ?? getTextContent(children);
+ let slug = headingSlugger.slug(textContent);
+
+ if (slug.endsWith('-')) slug = slug.slice(0, -1);
+ return slug;
+}
+
+type HeadingIdConfig = AstroMarkdocConfig<{
+ headingSlugger: Slugger;
+}>;
+
+/*
+ Expose standalone node for users to import in their config.
+ Allows users to apply a custom `render: AstroComponent`
+ and spread our default heading attributes.
+*/
+export const heading: Schema = {
+ children: ['inline'],
+ attributes: {
+ id: { type: String },
+ level: { type: Number, required: true, default: 1 },
+ },
+ transform(node, config: HeadingIdConfig) {
+ const { level, ...attributes } = node.transformAttributes(config);
+ const children = node.transformChildren(config);
+
+ if (!config.ctx?.headingSlugger) {
+ throw new MarkdocError({
+ message:
+ 'Unexpected problem adding heading IDs to Markdoc file. Did you modify the `ctx.headingSlugger` property in your Markdoc config?',
+ });
+ }
+ const slug = getSlug(attributes, children, config.ctx.headingSlugger);
+
+ const render = config.nodes?.heading?.render ?? `h${level}`;
+ const tagProps =
+ // For components, pass down `level` as a prop,
+ // alongside `__collectHeading` for our `headings` collector.
+ // Avoid accidentally rendering `level` as an HTML attribute otherwise!
+ typeof render === 'function'
+ ? { ...attributes, id: slug, __collectHeading: true, level }
+ : { ...attributes, id: slug };
+
+ return new Markdoc.Tag(render, tagProps, children);
+ },
+};
+
+// Called internally to ensure `ctx` is generated per-file, instead of per-build.
+export function setupHeadingConfig(): HeadingIdConfig {
+ const headingSlugger = new Slugger();
+ return {
+ ctx: {
+ headingSlugger,
+ },
+ nodes: {
+ heading,
+ },
+ };
+}