diff options
author | 2023-05-24 16:52:22 -0400 | |
---|---|---|
committer | 2023-05-24 16:52:22 -0400 | |
commit | 1efaef6be0265c68eac706623778e8ad23b33247 (patch) | |
tree | 3002f8ee7580157b7cb7df7e8741aa1c0b03448b /packages/integrations/markdoc/src/heading-ids.ts | |
parent | 7851f9258fae2f54795470253df9ce4bcd5f9cb0 (diff) | |
download | astro-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 '')
-rw-r--r-- | packages/integrations/markdoc/src/heading-ids.ts (renamed from packages/integrations/markdoc/src/nodes/heading.ts) | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/packages/integrations/markdoc/src/nodes/heading.ts b/packages/integrations/markdoc/src/heading-ids.ts index 0210e9b90..57b84d059 100644 --- a/packages/integrations/markdoc/src/nodes/heading.ts +++ b/packages/integrations/markdoc/src/heading-ids.ts @@ -1,13 +1,8 @@ import Markdoc, { type ConfigType, type RenderableTreeNode, type Schema } from '@markdoc/markdoc'; import Slugger from 'github-slugger'; -import { getTextContent } from '../runtime.js'; - -type ConfigTypeWithCtx = ConfigType & { - // TODO: decide on `ctx` as a convention for config merging - ctx: { - headingSlugger: Slugger; - }; -}; +import { getTextContent } from './runtime.js'; +import type { AstroMarkdocConfig } from './config.js'; +import { MarkdocError } from './utils.js'; function getSlug( attributes: Record<string, any>, @@ -24,16 +19,31 @@ function getSlug( 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: ConfigTypeWithCtx) { + 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}`; @@ -49,9 +59,9 @@ export const heading: Schema = { }, }; -export function setupHeadingConfig(): ConfigTypeWithCtx { +// Called internally to ensure `ctx` is generated per-file, instead of per-build. +export function setupHeadingConfig(): HeadingIdConfig { const headingSlugger = new Slugger(); - return { ctx: { headingSlugger, |