@astrojs/markdoc 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@ import { isValidUrl, MarkdocError, parseFrontmatter, prependForwardSlash } from
5
5
  import { emitESMImage } from "astro/assets";
6
6
  import { bold, red, yellow } from "kleur/colors";
7
7
  import { loadMarkdocConfig } from "./load-config.js";
8
- import { applyDefaultConfig } from "./runtime.js";
8
+ import { setupConfig } from "./runtime.js";
9
9
  function markdocIntegration(legacyConfig) {
10
10
  if (legacyConfig) {
11
11
  console.log(
@@ -38,7 +38,7 @@ function markdocIntegration(legacyConfig) {
38
38
  async getRenderModule({ entry, viteId }) {
39
39
  const ast = Markdoc.parse(entry.body);
40
40
  const pluginContext = this;
41
- const markdocConfig = applyDefaultConfig(userMarkdocConfig, entry);
41
+ const markdocConfig = setupConfig(userMarkdocConfig, entry);
42
42
  const validationErrors = Markdoc.validate(ast, markdocConfig).filter((e) => {
43
43
  return (
44
44
  // Ignore `variable-undefined` errors.
@@ -71,7 +71,7 @@ function markdocIntegration(legacyConfig) {
71
71
  }
72
72
  const res = `import { jsx as h } from 'astro/jsx-runtime';
73
73
  import { Renderer } from '@astrojs/markdoc/components';
74
- import { collectHeadings, applyDefaultConfig, Markdoc, headingSlugger } from '@astrojs/markdoc/runtime';
74
+ import { collectHeadings, setupConfig, Markdoc } from '@astrojs/markdoc/runtime';
75
75
  import * as entry from ${JSON.stringify(viteId + "?astroContentCollectionEntry")};
76
76
  ${markdocConfigResult ? `import _userConfig from ${JSON.stringify(
77
77
  markdocConfigResult.fileUrl.pathname
@@ -88,16 +88,14 @@ export function getHeadings() {
88
88
  TODO: propose new `render()` API to allow Markdoc variable passing to `render()` itself,
89
89
  instead of the Content component. Would remove double-transform and unlock variable resolution in heading slugs. */
90
90
  ""}
91
- headingSlugger.reset();
92
91
  const headingConfig = userConfig.nodes?.heading;
93
- const config = applyDefaultConfig(headingConfig ? { nodes: { heading: headingConfig } } : {}, entry);
92
+ const config = setupConfig(headingConfig ? { nodes: { heading: headingConfig } } : {}, entry);
94
93
  const ast = Markdoc.Ast.fromJSON(stringifiedAst);
95
94
  const content = Markdoc.transform(ast, config);
96
95
  return collectHeadings(Array.isArray(content) ? content : content.children);
97
96
  }
98
97
  export async function Content (props) {
99
- headingSlugger.reset();
100
- const config = applyDefaultConfig({
98
+ const config = setupConfig({
101
99
  ...userConfig,
102
100
  variables: { ...userConfig.variables, ...props },
103
101
  }, entry);
@@ -1,4 +1,10 @@
1
- import { type Schema } from '@markdoc/markdoc';
1
+ import { type ConfigType, type Schema } from '@markdoc/markdoc';
2
2
  import Slugger from 'github-slugger';
3
- export declare const headingSlugger: Slugger;
3
+ type ConfigTypeWithCtx = ConfigType & {
4
+ ctx: {
5
+ headingSlugger: Slugger;
6
+ };
7
+ };
4
8
  export declare const heading: Schema;
9
+ export declare function setupHeadingConfig(): ConfigTypeWithCtx;
10
+ export {};
@@ -1,8 +1,7 @@
1
1
  import Markdoc from "@markdoc/markdoc";
2
2
  import Slugger from "github-slugger";
3
3
  import { getTextContent } from "../runtime.js";
4
- const headingSlugger = new Slugger();
5
- function getSlug(attributes, children) {
4
+ function getSlug(attributes, children, headingSlugger) {
6
5
  if (attributes.id && typeof attributes.id === "string") {
7
6
  return attributes.id;
8
7
  }
@@ -22,7 +21,7 @@ const heading = {
22
21
  var _a, _b;
23
22
  const { level, ...attributes } = node.transformAttributes(config);
24
23
  const children = node.transformChildren(config);
25
- const slug = getSlug(attributes, children);
24
+ const slug = getSlug(attributes, children, config.ctx.headingSlugger);
26
25
  const render = ((_b = (_a = config.nodes) == null ? void 0 : _a.heading) == null ? void 0 : _b.render) ?? `h${level}`;
27
26
  const tagProps = (
28
27
  // For components, pass down `level` as a prop,
@@ -33,7 +32,18 @@ const heading = {
33
32
  return new Markdoc.Tag(render, tagProps, children);
34
33
  }
35
34
  };
35
+ function setupHeadingConfig() {
36
+ const headingSlugger = new Slugger();
37
+ return {
38
+ ctx: {
39
+ headingSlugger
40
+ },
41
+ nodes: {
42
+ heading
43
+ }
44
+ };
45
+ }
36
46
  export {
37
47
  heading,
38
- headingSlugger
48
+ setupHeadingConfig
39
49
  };
@@ -1,4 +1,4 @@
1
- export { headingSlugger } from './heading.js';
1
+ export { setupHeadingConfig } from './heading.js';
2
2
  export declare const nodes: {
3
3
  heading: import("@markdoc/markdoc").Schema;
4
4
  };
@@ -1,7 +1,7 @@
1
1
  import { heading } from "./heading.js";
2
- import { headingSlugger } from "./heading.js";
2
+ import { setupHeadingConfig } from "./heading.js";
3
3
  const nodes = { heading };
4
4
  export {
5
- headingSlugger,
6
- nodes
5
+ nodes,
6
+ setupHeadingConfig
7
7
  };
package/dist/runtime.d.ts CHANGED
@@ -1,10 +1,13 @@
1
1
  import type { MarkdownHeading } from '@astrojs/markdown-remark';
2
2
  import { type ConfigType as MarkdocConfig, type RenderableTreeNode } from '@markdoc/markdoc';
3
3
  import type { ContentEntryModule } from 'astro';
4
- /** Used to reset Slugger cache on each build at runtime */
4
+ /** Used to call `Markdoc.transform()` and `Markdoc.Ast` in runtime modules */
5
5
  export { default as Markdoc } from '@markdoc/markdoc';
6
- export { headingSlugger } from './nodes/index.js';
7
- export declare function applyDefaultConfig(config: MarkdocConfig, entry: ContentEntryModule): MarkdocConfig;
6
+ /**
7
+ * Merge user config with default config and set up context (ex. heading ID slugger)
8
+ * Called on each file's individual transform
9
+ */
10
+ export declare function setupConfig(userConfig: MarkdocConfig, entry: ContentEntryModule): MarkdocConfig;
8
11
  /**
9
12
  * Get text content as a string from a Markdoc transform AST
10
13
  */
package/dist/runtime.js CHANGED
@@ -1,19 +1,34 @@
1
1
  import Markdoc from "@markdoc/markdoc";
2
- import { nodes as astroNodes } from "./nodes/index.js";
2
+ import { setupHeadingConfig } from "./nodes/index.js";
3
3
  import { default as default2 } from "@markdoc/markdoc";
4
- import { headingSlugger } from "./nodes/index.js";
5
- function applyDefaultConfig(config, entry) {
4
+ function setupConfig(userConfig, entry) {
5
+ const defaultConfig = {
6
+ // `setupXConfig()` could become a "plugin" convention as well?
7
+ ...setupHeadingConfig(),
8
+ variables: { entry }
9
+ };
10
+ return mergeConfig(defaultConfig, userConfig);
11
+ }
12
+ function mergeConfig(configA, configB) {
6
13
  return {
7
- ...config,
8
- variables: {
9
- entry,
10
- ...config.variables
14
+ ...configA,
15
+ ...configB,
16
+ tags: {
17
+ ...configA.tags,
18
+ ...configB.tags
11
19
  },
12
20
  nodes: {
13
- ...astroNodes,
14
- ...config.nodes
21
+ ...configA.nodes,
22
+ ...configB.nodes
23
+ },
24
+ functions: {
25
+ ...configA.functions,
26
+ ...configB.functions
27
+ },
28
+ variables: {
29
+ ...configA.variables,
30
+ ...configB.variables
15
31
  }
16
- // TODO: Syntax highlighting
17
32
  };
18
33
  }
19
34
  function getTextContent(childNodes) {
@@ -56,8 +71,7 @@ function collectHeadings(children) {
56
71
  }
57
72
  export {
58
73
  default2 as Markdoc,
59
- applyDefaultConfig,
60
74
  collectHeadings,
61
75
  getTextContent,
62
- headingSlugger
76
+ setupConfig
63
77
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@astrojs/markdoc",
3
3
  "description": "Add support for Markdoc pages in your Astro site",
4
- "version": "0.2.0",
4
+ "version": "0.2.1",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -40,7 +40,7 @@
40
40
  "zod": "^3.17.3"
41
41
  },
42
42
  "peerDependencies": {
43
- "astro": "^2.5.0"
43
+ "astro": "^2.5.1"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@astrojs/markdown-remark": "^2.2.1",
@@ -53,7 +53,7 @@
53
53
  "mocha": "^9.2.2",
54
54
  "rollup": "^3.20.1",
55
55
  "vite": "^4.3.1",
56
- "astro": "2.5.0",
56
+ "astro": "2.5.1",
57
57
  "astro-scripts": "0.0.14"
58
58
  },
59
59
  "engines": {