@alloy-js/markdown 0.26.0 → 0.27.0-dev.5

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.
Files changed (106) hide show
  1. package/README.md +45 -0
  2. package/dist/dev/src/components/Code.js +20 -5
  3. package/dist/dev/src/components/Code.js.map +1 -1
  4. package/dist/dev/src/components/Heading.js +66 -2
  5. package/dist/dev/src/components/Heading.js.map +1 -1
  6. package/dist/dev/src/components/Link.js +33 -1
  7. package/dist/dev/src/components/Link.js.map +1 -1
  8. package/dist/dev/src/components/SourceFile.js +27 -6
  9. package/dist/dev/src/components/SourceFile.js.map +1 -1
  10. package/dist/dev/src/components/index.js +2 -0
  11. package/dist/dev/src/components/index.js.map +1 -1
  12. package/dist/dev/src/components/inline-code.js +28 -0
  13. package/dist/dev/src/components/inline-code.js.map +1 -0
  14. package/dist/dev/src/components/table.js +98 -0
  15. package/dist/dev/src/components/table.js.map +1 -0
  16. package/dist/dev/src/context/source-file.js +50 -0
  17. package/dist/dev/src/context/source-file.js.map +1 -0
  18. package/dist/dev/src/context/table.js +8 -0
  19. package/dist/dev/src/context/table.js.map +1 -0
  20. package/dist/dev/src/symbols/heading-symbol.js +22 -0
  21. package/dist/dev/src/symbols/heading-symbol.js.map +1 -0
  22. package/dist/dev/test/Code.test.js +150 -6
  23. package/dist/dev/test/Code.test.js.map +1 -1
  24. package/dist/dev/test/heading.test.js +774 -0
  25. package/dist/dev/test/heading.test.js.map +1 -0
  26. package/dist/dev/test/inline-code.test.js +236 -0
  27. package/dist/dev/test/inline-code.test.js.map +1 -0
  28. package/dist/dev/test/table.test.js +499 -0
  29. package/dist/dev/test/table.test.js.map +1 -0
  30. package/dist/dev/test/vitest.d.js.map +1 -1
  31. package/dist/src/components/Code.d.ts +10 -3
  32. package/dist/src/components/Code.d.ts.map +1 -1
  33. package/dist/src/components/Code.js +19 -4
  34. package/dist/src/components/Code.js.map +1 -1
  35. package/dist/src/components/Heading.d.ts +10 -2
  36. package/dist/src/components/Heading.d.ts.map +1 -1
  37. package/dist/src/components/Heading.js +54 -2
  38. package/dist/src/components/Heading.js.map +1 -1
  39. package/dist/src/components/Link.d.ts +9 -1
  40. package/dist/src/components/Link.d.ts.map +1 -1
  41. package/dist/src/components/Link.js +33 -1
  42. package/dist/src/components/Link.js.map +1 -1
  43. package/dist/src/components/SourceFile.d.ts.map +1 -1
  44. package/dist/src/components/SourceFile.js +17 -4
  45. package/dist/src/components/SourceFile.js.map +1 -1
  46. package/dist/src/components/index.d.ts +2 -0
  47. package/dist/src/components/index.d.ts.map +1 -1
  48. package/dist/src/components/index.js +2 -0
  49. package/dist/src/components/index.js.map +1 -1
  50. package/dist/src/components/inline-code.d.ts +16 -0
  51. package/dist/src/components/inline-code.d.ts.map +1 -0
  52. package/dist/src/components/inline-code.js +28 -0
  53. package/dist/src/components/inline-code.js.map +1 -0
  54. package/dist/src/components/table.d.ts +37 -0
  55. package/dist/src/components/table.d.ts.map +1 -0
  56. package/dist/src/components/table.js +74 -0
  57. package/dist/src/components/table.js.map +1 -0
  58. package/dist/src/context/source-file.d.ts +19 -0
  59. package/dist/src/context/source-file.d.ts.map +1 -0
  60. package/dist/src/context/source-file.js +50 -0
  61. package/dist/src/context/source-file.js.map +1 -0
  62. package/dist/src/context/table.d.ts +4 -0
  63. package/dist/src/context/table.d.ts.map +1 -0
  64. package/dist/src/context/table.js +8 -0
  65. package/dist/src/context/table.js.map +1 -0
  66. package/dist/src/symbols/heading-symbol.d.ts +14 -0
  67. package/dist/src/symbols/heading-symbol.d.ts.map +1 -0
  68. package/dist/src/symbols/heading-symbol.js +22 -0
  69. package/dist/src/symbols/heading-symbol.js.map +1 -0
  70. package/dist/test/Code.test.js +107 -3
  71. package/dist/test/Code.test.js.map +1 -1
  72. package/dist/test/heading.test.d.ts +2 -0
  73. package/dist/test/heading.test.d.ts.map +1 -0
  74. package/dist/test/heading.test.js +494 -0
  75. package/dist/test/heading.test.js.map +1 -0
  76. package/dist/test/inline-code.test.d.ts +2 -0
  77. package/dist/test/inline-code.test.d.ts.map +1 -0
  78. package/dist/test/inline-code.test.js +148 -0
  79. package/dist/test/inline-code.test.js.map +1 -0
  80. package/dist/test/table.test.d.ts +2 -0
  81. package/dist/test/table.test.d.ts.map +1 -0
  82. package/dist/test/table.test.js +303 -0
  83. package/dist/test/table.test.js.map +1 -0
  84. package/dist/test/vitest.d.js.map +1 -1
  85. package/dist/tsconfig.tsbuildinfo +1 -1
  86. package/docs/index.md +92 -0
  87. package/docs/tables.md +81 -0
  88. package/package.json +12 -9
  89. package/src/components/Code.tsx +35 -8
  90. package/src/components/Heading.tsx +103 -4
  91. package/src/components/Link.tsx +61 -2
  92. package/src/components/SourceFile.tsx +19 -2
  93. package/src/components/index.ts +2 -0
  94. package/src/components/inline-code.tsx +51 -0
  95. package/src/components/table.tsx +113 -0
  96. package/src/context/source-file.ts +79 -0
  97. package/src/context/table.ts +8 -0
  98. package/src/symbols/heading-symbol.ts +38 -0
  99. package/test/Code.test.tsx +102 -2
  100. package/test/heading.test.tsx +464 -0
  101. package/test/inline-code.test.tsx +166 -0
  102. package/test/table.test.tsx +282 -0
  103. package/test/vitest.d.ts +4 -1
  104. package/api-extractor.json +0 -4
  105. package/temp/api.json +0 -1361
  106. package/tsdoc-metadata.json +0 -11
package/docs/index.md ADDED
@@ -0,0 +1,92 @@
1
+ # Markdown
2
+
3
+ `@alloy-js/markdown` emits Markdown using Alloy components. Wrap each document in
4
+ the package's `SourceFile`, inside a core `Output`.
5
+
6
+ ## Heading references
7
+
8
+ Give a `Heading` a `refkey` to make it a link target. A refkey used as content
9
+ becomes a Markdown link; use `Link` directly to override the link text.
10
+ References can appear before their targets, including in other files.
11
+
12
+ ```tsx
13
+ import { Output, refkey } from "@alloy-js/core";
14
+ import { Heading, Link, SourceFile } from "@alloy-js/markdown";
15
+
16
+ const api = refkey();
17
+
18
+ <Output>
19
+ <SourceFile path="index.md">
20
+ <Link refkey={api} title="API reference" />
21
+ </SourceFile>
22
+ <SourceFile path="reference/api.md">
23
+ <Heading refkey={api} id="api">
24
+ Public API
25
+ </Heading>
26
+ {api}
27
+ </SourceFile>
28
+ </Output>;
29
+ ```
30
+
31
+ The first file contains `[API reference](reference/api.md#api)`. The second
32
+ contains:
33
+
34
+ ```md
35
+ <a id="api"></a>
36
+
37
+ # Public API
38
+
39
+ [Public API](#api)
40
+ ```
41
+
42
+ Same-file references use fragments only. Cross-file references use paths
43
+ relative to the referencing file's directory. File extensions are preserved;
44
+ path segments and fragments are URL-encoded. Missing refkeys produce Alloy's
45
+ standard unresolved-symbol placeholder and warning.
46
+
47
+ ## Anchors and collisions
48
+
49
+ Without `id`, a heading's anchor is the GitHub-style slug of its string
50
+ children: the trimmed text is lowercased, punctuation is removed, and spaces
51
+ become hyphens. For example, `Model Foo.Bar` becomes `model-foobar`. Strings
52
+ are treated as plain text. If they contain Markdown or HTML syntax, the
53
+ rendered heading text can differ from the slug input, so use `id`.
54
+
55
+ Headings with JSX children, such as `InlineCode` or nested components, have no
56
+ automatic anchor because Alloy does not inspect rendered JSX. To reference
57
+ one, give it an `id`, and give links to it a `title`. Otherwise rendering
58
+ throws an error. Such a heading still consumes a renderer-generated slug that
59
+ Alloy cannot determine. Subsequent referenced headings in the file therefore
60
+ also require explicit IDs, even when their children are strings.
61
+
62
+ Repeated slugs receive `-1`, `-2`, and so on, skipping IDs already generated
63
+ by the slugger. Every `Heading` with string children counts, including
64
+ headings without refkeys and those emitted by `Section`. Each file has an
65
+ independent slug sequence. Headings are numbered in the order they are
66
+ rendered, so a heading inserted later before existing headings (for example
67
+ by `Show`) is numbered after them. If a referenced heading's slug is empty, an
68
+ explicit `section` anchor is emitted instead, suffixed if necessary to avoid
69
+ other IDs in the file.
70
+
71
+ Use `id` for a stable URL independent of display text. It is emitted as an
72
+ HTML anchor before the heading, rather than renderer-specific `{#id}` syntax.
73
+ IDs must be nonempty and cannot contain whitespace, quotes, `<`, `>`, or `&`.
74
+ Duplicate IDs, including collisions with automatic heading anchors, throw an
75
+ error rather than silently renaming an explicit ID. An explicit-ID heading
76
+ still consumes its automatic slug, since Markdown renderers also generate an
77
+ anchor for its heading text.
78
+
79
+ Anchors and default link text update with reactive string children, such as
80
+ `<Heading refkey={key}>{name.value}</Heading>`. Supply an explicit `id` when
81
+ a URL must remain stable even if display text or surrounding headings change.
82
+ Explicit IDs also follow reactive `id` props and are validated on each change.
83
+ Links follow reactive `refkey` props, including unresolved-reference diagnostics.
84
+
85
+ The default link label is the heading text, escaped for Markdown. Inside a
86
+ `TableCell`, pipes in that default label are also escaped. Explicit `title`
87
+ strings are emitted unchanged and require caller-owned escaping. Direct `Link`
88
+ calls with `href` and `title` retain their existing behavior.
89
+
90
+ Automatic anchors assume GitHub-style heading IDs. For other renderers, use
91
+ explicit IDs. Headings with JSX children, raw Markdown headings and raw HTML
92
+ anchors do not participate in collision tracking.
package/docs/tables.md ADDED
@@ -0,0 +1,81 @@
1
+ # Markdown tables
2
+
3
+ `Table`, `TableRow`, and `TableCell` generate GitHub-flavored Markdown tables.
4
+ Define the columns once, then compose rows and cells with ordinary Alloy JSX:
5
+
6
+ ```tsx
7
+ import { For } from "@alloy-js/core";
8
+ import { Table, TableCell, TableRow } from "@alloy-js/markdown";
9
+
10
+ <Table columns={[{ header: "Type" }, { header: "Description", align: "left" }]}>
11
+ <For each={properties}>
12
+ {(property) => (
13
+ <TableRow>
14
+ <TableCell>{property.type}</TableCell>
15
+ <TableCell>{property.description}</TableCell>
16
+ </TableRow>
17
+ )}
18
+ </For>
19
+ </Table>;
20
+ ```
21
+
22
+ Provide at least one column and exactly one cell per column in each row. Empty
23
+ cells are supported. Row widths are not validated. Column alignment can be
24
+ `"left"`, `"center"`, or `"right"`; omit it for the renderer's default alignment.
25
+ When using `For` to compose cells within a row, set `joiner=""`.
26
+
27
+ ## Direct strings and JSX
28
+
29
+ Direct string children are escaped as literal text. Pipes do not create extra
30
+ columns, angle brackets and ampersands remain visible, and Markdown punctuation
31
+ such as backticks or emphasis markers is displayed as written:
32
+
33
+ ```tsx
34
+ <TableCell>{"A | B <T> & `literal backticks`"}</TableCell>
35
+ ```
36
+
37
+ Strings within arrays or fragments receive the same escaping. JSX components
38
+ are passed through unchanged: the table does not render them separately,
39
+ inspect their output, or escape the Markdown they produce.
40
+
41
+ For inline formatting, use a JSX component that emits the desired Markdown.
42
+ **The caller owns escaping within that component**, including table pipes
43
+ inside code spans. For example:
44
+
45
+ ```tsx
46
+ import { code } from "@alloy-js/core";
47
+
48
+ function AcceptedTypes() {
49
+ return code`**Accepted:** \`A \\| B\``;
50
+ }
51
+
52
+ <TableCell>
53
+ <AcceptedTypes />
54
+ </TableCell>;
55
+ ```
56
+
57
+ This deliberately pre-escapes the pipe for the GFM table parser. It is not a
58
+ general-purpose inline-code serializer. Unescaped pipes emitted by JSX can
59
+ create extra columns. Column headings follow the same string/JSX rules.
60
+
61
+ All JSX output, including output from `Link`, is passed through unchanged.
62
+ Pre-escape pipes in link titles and other component output before using them
63
+ in a table cell or heading.
64
+
65
+ `InlineCode` handles its own direct-string pipes using the table context, so it
66
+ can be used in cells and column headings without pre-escaping those strings.
67
+ This does not change the pass-through behavior of arbitrary JSX.
68
+
69
+ ## Multiline and block content
70
+
71
+ Physical newlines in direct strings become `<br>` inside a cell; blank lines
72
+ become repeated breaks. This requires a renderer that supports inline HTML.
73
+
74
+ JSX output is not normalized. Physical line breaks emitted by a component can
75
+ end the table row, including formatting-induced breaks from components such as
76
+ `Prose`. Callers must keep JSX content on one physical line and emit any desired
77
+ inline breaks themselves.
78
+
79
+ GFM tables cannot contain block-level Markdown. Paragraphs, lists, headings,
80
+ fenced code blocks, and nested tables are not supported cell structure. Render
81
+ complex block content outside the table and link to it from a cell instead.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alloy-js/markdown",
3
- "version": "0.26.0",
3
+ "version": "0.27.0-dev.5",
4
4
  "description": "Alloy components for generating markdown",
5
5
  "repository": {
6
6
  "type": "git",
@@ -20,21 +20,24 @@
20
20
  "author": "brian.terlson@microsoft.com",
21
21
  "license": "MIT",
22
22
  "dependencies": {
23
- "yaml": "^2.7.1",
24
- "@alloy-js/core": "~0.26.0"
23
+ "@alloy-js/core": "~0.26.0 || >= 0.27.0-dev.1",
24
+ "github-slugger": "^2.0.0",
25
+ "pathe": "^2.0.3",
26
+ "yaml": "^2.7.1"
25
27
  },
26
28
  "devDependencies": {
27
- "@microsoft/api-extractor": "~7.58.8",
29
+ "@alloy-js/cli": "~0.26.0 || >= 0.27.0-dev.0",
30
+ "@alloy-js/rollup-plugin": "~0.2.0 || >= 0.2.1-dev.0",
31
+ "@alloy-js/typescript": "~0.26.0 || >= 0.27.0-dev.1",
28
32
  "@types/node": "^26.1.1",
33
+ "html-entities": "2.6.0",
34
+ "marked": "^18.0.5",
29
35
  "typescript": "^7.0.2",
30
- "vitest": "4.1.10",
31
- "@alloy-js/cli": "~0.26.0",
32
- "@alloy-js/rollup-plugin": "~0.2.0"
36
+ "vitest": "4.1.10"
33
37
  },
34
38
  "type": "module",
35
39
  "scripts": {
36
- "generate-docs": "api-extractor run",
37
- "build": "alloy build --with-dev && npm run generate-docs",
40
+ "build": "alloy build --with-dev",
38
41
  "clean": "rimraf dist/ .temp/",
39
42
  "watch": "alloy build --watch",
40
43
  "test": "vitest run"
@@ -1,24 +1,51 @@
1
- import { List } from "@alloy-js/core";
1
+ import { computed, List, memo } from "@alloy-js/core";
2
2
  import type { Children } from "@alloy-js/core/jsx-runtime";
3
3
 
4
4
  export interface CodeProps {
5
- /** Language of the code block */
5
+ /**
6
+ * Optional Markdown info string (language followed by optional metadata).
7
+ * Must be a single line. Backticks are supported by using tilde fences.
8
+ */
6
9
  lang?: string;
7
10
 
8
- /** Body of the code block */
11
+ /**
12
+ * Body of the code block. Direct strings determine the fence length.
13
+ * JSX children render unchanged and must not contain closing fences.
14
+ */
9
15
  children?: Children;
10
16
  }
11
17
 
12
18
  /**
13
- * Render a Markdown code block
19
+ * Render a Markdown code block. Direct string bodies use content-safe fences.
20
+ * JSX children render normally without being inspected for fence conflicts.
14
21
  */
15
22
  export function Code(props: CodeProps) {
16
- const tripleBackticks = "```";
23
+ const body = memo(() => props.children);
24
+ const info = computed(() => {
25
+ const info = props.lang ?? "";
26
+ if (/[\r\n]/.test(info)) {
27
+ throw new Error("Code lang must be a single-line Markdown info string.");
28
+ }
29
+ return info.trim();
30
+ });
31
+ const fence = computed(() => {
32
+ const marker = info.value.includes("`") ? "~" : "`";
33
+ const content = body();
34
+ const runs =
35
+ typeof content === "string"
36
+ ? (content.match(marker === "`" ? /`+/g : /~+/g) ?? [])
37
+ : [];
38
+ const length = runs.reduce(
39
+ (length, run) => Math.max(length, run.length + 1),
40
+ 3,
41
+ );
42
+ return marker.repeat(length);
43
+ });
17
44
  return (
18
45
  <List>
19
- {tripleBackticks + props.lang}
20
- {props.children}
21
- {tripleBackticks}
46
+ {fence.value + info.value}
47
+ {body()}
48
+ {fence.value}
22
49
  </List>
23
50
  );
24
51
  }
@@ -1,20 +1,119 @@
1
- import type { Refkey } from "@alloy-js/core";
2
- import type { Children } from "@alloy-js/core/jsx-runtime";
1
+ import {
2
+ BasicScope,
3
+ childrenArray,
4
+ code,
5
+ createSymbol,
6
+ Declaration,
7
+ memo,
8
+ onCleanup,
9
+ shallowRef,
10
+ SourceFileContext,
11
+ useContext,
12
+ useScope,
13
+ type Children,
14
+ type Refkey,
15
+ } from "@alloy-js/core";
3
16
 
4
17
  import { useSectionContext } from "../context/section.js";
18
+ import {
19
+ MarkdownFileContext,
20
+ type HeadingEntry,
21
+ } from "../context/source-file.js";
22
+ import { HeadingSymbol } from "../symbols/heading-symbol.js";
5
23
 
6
24
  export interface HeadingProps {
25
+ /** Heading depth. Defaults to the current section depth. */
7
26
  level?: number;
27
+ /**
28
+ * Heading content. Automatic IDs and default link text are derived from
29
+ * string children; headings with JSX children need an explicit `id` to be
30
+ * referenced.
31
+ */
8
32
  children: Children;
33
+ /** Key used to link to this heading. */
9
34
  refkey?: Refkey;
35
+ /** Stable anchor ID independent of the display text. Must be unique in the file. */
36
+ id?: string;
10
37
  }
11
38
 
12
39
  export function Heading(props: HeadingProps) {
13
40
  const level =
14
41
  props.level !== undefined ? props.level : useSectionContext().level;
15
- return (
42
+ const children = memo(() => childrenArray(() => props.children));
43
+ const file = useContext(MarkdownFileContext);
44
+ const sourceFile = useContext(SourceFileContext);
45
+ const scope = useScope();
46
+ if (!file || !sourceFile || !(scope instanceof BasicScope)) {
47
+ if (props.refkey === undefined && props.id === undefined) {
48
+ return (
49
+ <>
50
+ {"#".repeat(level)} {children}
51
+ </>
52
+ );
53
+ }
54
+ throw new Error("Heading must be rendered inside a Markdown SourceFile.");
55
+ }
56
+
57
+ const text = memo(() => headingText(children()));
58
+ const symbol =
59
+ props.refkey === undefined
60
+ ? undefined
61
+ : createSymbol(HeadingSymbol, props.id ?? "", scope.symbols, {
62
+ refkeys: props.refkey,
63
+ ignoreNameConflict: true,
64
+ path: sourceFile.path,
65
+ text,
66
+ });
67
+ const entry: HeadingEntry = {
68
+ text,
69
+ get id() {
70
+ const id = props.id;
71
+ if (id !== undefined && (id.length === 0 || /[\s"'<>&]/u.test(id))) {
72
+ throw new Error(`Invalid Markdown heading ID: ${JSON.stringify(id)}.`);
73
+ }
74
+ return id;
75
+ },
76
+ symbol,
77
+ anchor: shallowRef<string>(),
78
+ };
79
+ file.headings.add(entry);
80
+ onCleanup(() => {
81
+ file.headings.delete(entry);
82
+ symbol?.delete();
83
+ });
84
+ const heading = (
16
85
  <>
17
- {"#".repeat(level)} {props.children}
86
+ {() =>
87
+ entry.anchor.value !== undefined && (
88
+ <>
89
+ {code`<a id="${entry.anchor.value}"></a>`}
90
+ <hbr />
91
+ <hbr />
92
+ </>
93
+ )
94
+ }
95
+ {"#".repeat(level)} {children}
18
96
  </>
19
97
  );
98
+ return symbol ? (
99
+ <Declaration symbol={symbol}>{heading}</Declaration>
100
+ ) : (
101
+ heading
102
+ );
103
+ }
104
+
105
+ function headingText(children: Children[]): string | undefined {
106
+ let text = "";
107
+ for (const child of children) {
108
+ if (typeof child === "string" || typeof child === "number") {
109
+ text += child;
110
+ } else if (
111
+ child !== null &&
112
+ child !== undefined &&
113
+ typeof child !== "boolean"
114
+ ) {
115
+ return undefined;
116
+ }
117
+ }
118
+ return text.trim();
20
119
  }
@@ -1,11 +1,29 @@
1
- import type { Refkey } from "@alloy-js/core";
1
+ import {
2
+ resolve,
3
+ SourceFileContext,
4
+ unresolvedRefkey,
5
+ useContext,
6
+ type Refkey,
7
+ } from "@alloy-js/core";
8
+ import { dirname, relative } from "pathe";
9
+
10
+ import { useTableContext } from "../context/table.js";
11
+ import { HeadingSymbol } from "../symbols/heading-symbol.js";
2
12
 
3
13
  export interface LinkPropsWithRefkey {
14
+ /** Key of the target heading, which may be declared later or in another file. */
4
15
  refkey: Refkey;
16
+ /**
17
+ * Override the link text. Defaults to the target heading's text, which
18
+ * requires the heading to have string children.
19
+ */
20
+ title?: string;
5
21
  }
6
22
 
7
23
  export interface LinkPropsWithHref {
24
+ /** Link destination. */
8
25
  href: string;
26
+ /** Link text. */
9
27
  title: string;
10
28
  }
11
29
 
@@ -13,7 +31,30 @@ export type LinkProps = LinkPropsWithRefkey | LinkPropsWithHref;
13
31
 
14
32
  export function Link(props: LinkProps) {
15
33
  if ("refkey" in props) {
16
- return "refkey";
34
+ const sourceFile = useContext(SourceFileContext);
35
+ if (!sourceFile) {
36
+ throw new Error("Link references must be rendered inside a SourceFile.");
37
+ }
38
+ const table = useTableContext();
39
+ return () => {
40
+ const target = resolve(props.refkey);
41
+ if (!target.value) {
42
+ return unresolvedRefkey(props.refkey);
43
+ }
44
+ const symbol = target.value.symbol;
45
+ if (!(symbol instanceof HeadingSymbol)) {
46
+ throw new Error("Markdown Link refkey must reference a Heading.");
47
+ }
48
+ const path =
49
+ sourceFile.path === symbol.path
50
+ ? ""
51
+ : relative(dirname(sourceFile.path), symbol.path)
52
+ .split("/")
53
+ .map(encodeUrlPart)
54
+ .join("/");
55
+ const href = `${path}#${encodeUrlPart(symbol.name)}`;
56
+ return `[${props.title ?? defaultTitle(symbol, table)}](${href})`;
57
+ };
17
58
  }
18
59
 
19
60
  return (
@@ -22,3 +63,21 @@ export function Link(props: LinkProps) {
22
63
  </>
23
64
  );
24
65
  }
66
+
67
+ function defaultTitle(symbol: HeadingSymbol, table: boolean): string {
68
+ const text = symbol.text();
69
+ if (text === undefined) {
70
+ throw new Error(
71
+ `Link to Markdown heading ${JSON.stringify(symbol.name)} requires a title because the heading has non-string children.`,
72
+ );
73
+ }
74
+ const title = text.replace(/[\\`*_[\]<>&~]/g, "\\$&");
75
+ return table ? title.replace(/\|/g, "\\|") : title;
76
+ }
77
+
78
+ function encodeUrlPart(value: string): string {
79
+ return encodeURIComponent(value).replace(
80
+ /[!'()*]/g,
81
+ (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`,
82
+ );
83
+ }
@@ -1,6 +1,17 @@
1
1
  import type { Children } from "@alloy-js/core";
2
- import { SourceFile as CoreSourceFile, List } from "@alloy-js/core";
2
+ import {
3
+ BasicScope,
4
+ SourceFile as CoreSourceFile,
5
+ createScope,
6
+ List,
7
+ Scope,
8
+ useScope,
9
+ } from "@alloy-js/core";
3
10
 
11
+ import {
12
+ createMarkdownFileContext,
13
+ MarkdownFileContext,
14
+ } from "../context/source-file.js";
4
15
  import { Link } from "./Link.jsx";
5
16
 
6
17
  export interface SourceFileProps {
@@ -8,9 +19,15 @@ export interface SourceFileProps {
8
19
  children: Children;
9
20
  }
10
21
  export function SourceFile(props: SourceFileProps) {
22
+ const scope = createScope(BasicScope, props.path, useScope());
23
+ const file = createMarkdownFileContext();
11
24
  return (
12
25
  <CoreSourceFile path={props.path} filetype="md" reference={Link}>
13
- <List doubleHardline>{props.children}</List>
26
+ <Scope value={scope}>
27
+ <MarkdownFileContext.Provider value={file}>
28
+ <List doubleHardline>{props.children}</List>
29
+ </MarkdownFileContext.Provider>
30
+ </Scope>
14
31
  </CoreSourceFile>
15
32
  );
16
33
  }
@@ -1,7 +1,9 @@
1
1
  export * from "./Code.jsx";
2
2
  export * from "./Frontmatter.jsx";
3
3
  export * from "./Heading.jsx";
4
+ export * from "./inline-code.jsx";
4
5
  export * from "./Link.jsx";
5
6
  export * from "./List.jsx";
6
7
  export * from "./Section.jsx";
7
8
  export * from "./SourceFile.jsx";
9
+ export * from "./table.jsx";
@@ -0,0 +1,51 @@
1
+ import type { Children } from "@alloy-js/core";
2
+
3
+ import { useTableContext } from "../context/table.js";
4
+
5
+ export interface InlineCodeProps {
6
+ /**
7
+ * Direct strings are escaped automatically. JSX and other non-string
8
+ * children are wrapped in single backticks unchanged; callers own escaping.
9
+ */
10
+ children?: Children;
11
+ }
12
+
13
+ /**
14
+ * Render a Markdown code span. Direct strings use adaptive delimiters and
15
+ * padding, with each CRLF, CR, or LF replaced by one space.
16
+ * Empty strings and omitted content use inline HTML.
17
+ * Direct string pipes are escaped inside table cells and left literal elsewhere.
18
+ */
19
+ export function InlineCode(props: InlineCodeProps) {
20
+ const inTable = useTableContext();
21
+ return () => {
22
+ const content = props.children ?? "";
23
+ return typeof content === "string" ? (
24
+ inlineCode(inTable ? content.replace(/\|/g, "\\|") : content)
25
+ ) : (
26
+ <>
27
+ {"`"}
28
+ {content}
29
+ {"`"}
30
+ </>
31
+ );
32
+ };
33
+ }
34
+
35
+ function inlineCode(content: string): string {
36
+ const text = content.replace(/\r\n|\r|\n/g, " ");
37
+ // CommonMark has no empty code-span syntax.
38
+ if (text.length === 0) return "<code></code>";
39
+
40
+ let delimiterLength = 1;
41
+ for (const match of text.matchAll(/`+/g)) {
42
+ delimiterLength = Math.max(delimiterLength, match[0].length + 1);
43
+ }
44
+ const delimiter = "`".repeat(delimiterLength);
45
+ const needsPadding =
46
+ text.startsWith("`") ||
47
+ text.endsWith("`") ||
48
+ (text.startsWith(" ") && text.endsWith(" ") && /[^ ]/.test(text));
49
+ const padding = needsPadding ? " " : "";
50
+ return delimiter + padding + text + padding + delimiter;
51
+ }