@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.
- package/README.md +45 -0
- package/dist/dev/src/components/Code.js +20 -5
- package/dist/dev/src/components/Code.js.map +1 -1
- package/dist/dev/src/components/Heading.js +66 -2
- package/dist/dev/src/components/Heading.js.map +1 -1
- package/dist/dev/src/components/Link.js +33 -1
- package/dist/dev/src/components/Link.js.map +1 -1
- package/dist/dev/src/components/SourceFile.js +27 -6
- package/dist/dev/src/components/SourceFile.js.map +1 -1
- package/dist/dev/src/components/index.js +2 -0
- package/dist/dev/src/components/index.js.map +1 -1
- package/dist/dev/src/components/inline-code.js +28 -0
- package/dist/dev/src/components/inline-code.js.map +1 -0
- package/dist/dev/src/components/table.js +98 -0
- package/dist/dev/src/components/table.js.map +1 -0
- package/dist/dev/src/context/source-file.js +50 -0
- package/dist/dev/src/context/source-file.js.map +1 -0
- package/dist/dev/src/context/table.js +8 -0
- package/dist/dev/src/context/table.js.map +1 -0
- package/dist/dev/src/symbols/heading-symbol.js +22 -0
- package/dist/dev/src/symbols/heading-symbol.js.map +1 -0
- package/dist/dev/test/Code.test.js +150 -6
- package/dist/dev/test/Code.test.js.map +1 -1
- package/dist/dev/test/heading.test.js +774 -0
- package/dist/dev/test/heading.test.js.map +1 -0
- package/dist/dev/test/inline-code.test.js +236 -0
- package/dist/dev/test/inline-code.test.js.map +1 -0
- package/dist/dev/test/table.test.js +499 -0
- package/dist/dev/test/table.test.js.map +1 -0
- package/dist/dev/test/vitest.d.js.map +1 -1
- package/dist/src/components/Code.d.ts +10 -3
- package/dist/src/components/Code.d.ts.map +1 -1
- package/dist/src/components/Code.js +19 -4
- package/dist/src/components/Code.js.map +1 -1
- package/dist/src/components/Heading.d.ts +10 -2
- package/dist/src/components/Heading.d.ts.map +1 -1
- package/dist/src/components/Heading.js +54 -2
- package/dist/src/components/Heading.js.map +1 -1
- package/dist/src/components/Link.d.ts +9 -1
- package/dist/src/components/Link.d.ts.map +1 -1
- package/dist/src/components/Link.js +33 -1
- package/dist/src/components/Link.js.map +1 -1
- package/dist/src/components/SourceFile.d.ts.map +1 -1
- package/dist/src/components/SourceFile.js +17 -4
- package/dist/src/components/SourceFile.js.map +1 -1
- package/dist/src/components/index.d.ts +2 -0
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +2 -0
- package/dist/src/components/index.js.map +1 -1
- package/dist/src/components/inline-code.d.ts +16 -0
- package/dist/src/components/inline-code.d.ts.map +1 -0
- package/dist/src/components/inline-code.js +28 -0
- package/dist/src/components/inline-code.js.map +1 -0
- package/dist/src/components/table.d.ts +37 -0
- package/dist/src/components/table.d.ts.map +1 -0
- package/dist/src/components/table.js +74 -0
- package/dist/src/components/table.js.map +1 -0
- package/dist/src/context/source-file.d.ts +19 -0
- package/dist/src/context/source-file.d.ts.map +1 -0
- package/dist/src/context/source-file.js +50 -0
- package/dist/src/context/source-file.js.map +1 -0
- package/dist/src/context/table.d.ts +4 -0
- package/dist/src/context/table.d.ts.map +1 -0
- package/dist/src/context/table.js +8 -0
- package/dist/src/context/table.js.map +1 -0
- package/dist/src/symbols/heading-symbol.d.ts +14 -0
- package/dist/src/symbols/heading-symbol.d.ts.map +1 -0
- package/dist/src/symbols/heading-symbol.js +22 -0
- package/dist/src/symbols/heading-symbol.js.map +1 -0
- package/dist/test/Code.test.js +107 -3
- package/dist/test/Code.test.js.map +1 -1
- package/dist/test/heading.test.d.ts +2 -0
- package/dist/test/heading.test.d.ts.map +1 -0
- package/dist/test/heading.test.js +494 -0
- package/dist/test/heading.test.js.map +1 -0
- package/dist/test/inline-code.test.d.ts +2 -0
- package/dist/test/inline-code.test.d.ts.map +1 -0
- package/dist/test/inline-code.test.js +148 -0
- package/dist/test/inline-code.test.js.map +1 -0
- package/dist/test/table.test.d.ts +2 -0
- package/dist/test/table.test.d.ts.map +1 -0
- package/dist/test/table.test.js +303 -0
- package/dist/test/table.test.js.map +1 -0
- package/dist/test/vitest.d.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/docs/index.md +92 -0
- package/docs/tables.md +81 -0
- package/package.json +12 -9
- package/src/components/Code.tsx +35 -8
- package/src/components/Heading.tsx +103 -4
- package/src/components/Link.tsx +61 -2
- package/src/components/SourceFile.tsx +19 -2
- package/src/components/index.ts +2 -0
- package/src/components/inline-code.tsx +51 -0
- package/src/components/table.tsx +113 -0
- package/src/context/source-file.ts +79 -0
- package/src/context/table.ts +8 -0
- package/src/symbols/heading-symbol.ts +38 -0
- package/test/Code.test.tsx +102 -2
- package/test/heading.test.tsx +464 -0
- package/test/inline-code.test.tsx +166 -0
- package/test/table.test.tsx +282 -0
- package/test/vitest.d.ts +4 -1
- package/api-extractor.json +0 -4
- package/temp/api.json +0 -1361
- package/tsdoc-metadata.json +0 -11
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {
|
|
2
|
+
childrenArray,
|
|
3
|
+
code,
|
|
4
|
+
For,
|
|
5
|
+
List,
|
|
6
|
+
memo,
|
|
7
|
+
type Children,
|
|
8
|
+
} from "@alloy-js/core";
|
|
9
|
+
|
|
10
|
+
import { TableContext } from "../context/table.js";
|
|
11
|
+
|
|
12
|
+
export interface TableColumn {
|
|
13
|
+
/** Column heading. Direct strings are escaped; JSX is passed through. */
|
|
14
|
+
header: Children;
|
|
15
|
+
/** Alignment of the column's contents. */
|
|
16
|
+
align?: "left" | "center" | "right";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface TableProps {
|
|
20
|
+
/** Columns in output order. At least one column is required. */
|
|
21
|
+
columns: readonly TableColumn[];
|
|
22
|
+
/** TableRow components, with one TableCell per column. */
|
|
23
|
+
children?: Children;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Render a GFM table with a header and optional body rows.
|
|
28
|
+
*
|
|
29
|
+
* Direct string cells are escaped, with newlines converted to inline HTML
|
|
30
|
+
* breaks. JSX output is not inspected; callers own its escaping and layout.
|
|
31
|
+
*/
|
|
32
|
+
export function Table(props: TableProps) {
|
|
33
|
+
const rows = memo(() => childrenArray(() => props.children));
|
|
34
|
+
return () => {
|
|
35
|
+
if (props.columns.length === 0) {
|
|
36
|
+
throw new Error("Table requires at least one column.");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const delimiters = props.columns.map((column) =>
|
|
40
|
+
column.align === "center"
|
|
41
|
+
? ":---:"
|
|
42
|
+
: column.align === "right"
|
|
43
|
+
? "---:"
|
|
44
|
+
: column.align === "left"
|
|
45
|
+
? ":---"
|
|
46
|
+
: "---",
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<List>
|
|
51
|
+
<TableRow>
|
|
52
|
+
<For each={props.columns} joiner="">
|
|
53
|
+
{(column) => <TableCell>{column.header}</TableCell>}
|
|
54
|
+
</For>
|
|
55
|
+
</TableRow>
|
|
56
|
+
{code`| ${delimiters.join(" | ")} |`}
|
|
57
|
+
<List>{rows}</List>
|
|
58
|
+
</List>
|
|
59
|
+
);
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface TableRowProps {
|
|
64
|
+
/** TableCell components. Use an empty joiner when composing cells with For. */
|
|
65
|
+
children?: Children;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Render a single row. Provide one TableCell per column. */
|
|
69
|
+
export function TableRow(props: TableRowProps) {
|
|
70
|
+
return code`|${props.children}`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface TableCellProps {
|
|
74
|
+
/** Direct strings are escaped as literal text. JSX is passed through. */
|
|
75
|
+
children?: Children;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Escape direct string children without inspecting rendered JSX output.
|
|
80
|
+
* Context-aware Markdown components can escape their own pipes. Other JSX
|
|
81
|
+
* remains uninspected; callers own its escaping and physical line breaks.
|
|
82
|
+
*/
|
|
83
|
+
export function TableCell(props: TableCellProps) {
|
|
84
|
+
const content = memo(() =>
|
|
85
|
+
childrenArray(() => props.children).map((child) =>
|
|
86
|
+
typeof child === "string" ? escapeTableText(child) : child,
|
|
87
|
+
),
|
|
88
|
+
);
|
|
89
|
+
return (
|
|
90
|
+
<TableContext.Provider value={true}>
|
|
91
|
+
{code` ${content} |`}
|
|
92
|
+
</TableContext.Provider>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function escapeTableText(text: string): string {
|
|
97
|
+
return text.replace(/[&<>\\`*_[\]~|]|\r\n?|\n/g, (character) => {
|
|
98
|
+
switch (character) {
|
|
99
|
+
case "&":
|
|
100
|
+
return "&";
|
|
101
|
+
case "<":
|
|
102
|
+
return "<";
|
|
103
|
+
case ">":
|
|
104
|
+
return ">";
|
|
105
|
+
case "\r":
|
|
106
|
+
case "\n":
|
|
107
|
+
case "\r\n":
|
|
108
|
+
return "<br>";
|
|
109
|
+
default:
|
|
110
|
+
return `\\${character}`;
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
effect,
|
|
4
|
+
shallowReactive,
|
|
5
|
+
type Ref,
|
|
6
|
+
} from "@alloy-js/core";
|
|
7
|
+
import GithubSlugger from "github-slugger";
|
|
8
|
+
|
|
9
|
+
import type { HeadingSymbol } from "../symbols/heading-symbol.js";
|
|
10
|
+
|
|
11
|
+
export interface HeadingEntry {
|
|
12
|
+
/** Trimmed heading text, or `undefined` when the heading has JSX children. */
|
|
13
|
+
text: () => string | undefined;
|
|
14
|
+
/** Explicit anchor ID. */
|
|
15
|
+
id?: string;
|
|
16
|
+
/** Link target, present when the heading has a refkey. */
|
|
17
|
+
symbol?: HeadingSymbol;
|
|
18
|
+
/** HTML anchor emitted before the heading, if any. */
|
|
19
|
+
anchor: Ref<string | undefined>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface MarkdownFileContext {
|
|
23
|
+
/** Headings in the file, in mount order. */
|
|
24
|
+
headings: Set<HeadingEntry>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const MarkdownFileContext = createContext<MarkdownFileContext>();
|
|
28
|
+
|
|
29
|
+
export function createMarkdownFileContext(): MarkdownFileContext {
|
|
30
|
+
const headings = shallowReactive(new Set<HeadingEntry>());
|
|
31
|
+
effect(() => allocateHeadingIds([...headings]));
|
|
32
|
+
return { headings };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function allocateHeadingIds(headings: HeadingEntry[]) {
|
|
36
|
+
// Renderers generate an ID for every heading, including those with an explicit ID.
|
|
37
|
+
const slugger = new GithubSlugger();
|
|
38
|
+
let opaque = false;
|
|
39
|
+
const automatic = headings.map((heading) => {
|
|
40
|
+
const text = heading.text();
|
|
41
|
+
if (opaque && heading.symbol && heading.id === undefined) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
"A Heading with a refkey requires an explicit id after a heading with non-string children.",
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
if (text === undefined) opaque = true;
|
|
47
|
+
return text === undefined ? undefined : slugger.slug(text);
|
|
48
|
+
});
|
|
49
|
+
const ids = new Set(automatic.filter((id) => id !== undefined));
|
|
50
|
+
|
|
51
|
+
const explicit = new Set<string>();
|
|
52
|
+
headings.forEach((heading, i) => {
|
|
53
|
+
const id = heading.id;
|
|
54
|
+
if (id === undefined) return;
|
|
55
|
+
if (explicit.has(id) || (ids.has(id) && automatic[i] !== id)) {
|
|
56
|
+
throw new Error(`Duplicate Markdown heading ID: ${JSON.stringify(id)}.`);
|
|
57
|
+
}
|
|
58
|
+
explicit.add(id);
|
|
59
|
+
});
|
|
60
|
+
for (const id of explicit) ids.add(id);
|
|
61
|
+
|
|
62
|
+
headings.forEach((heading, i) => {
|
|
63
|
+
let anchor = heading.id;
|
|
64
|
+
if (anchor === undefined && automatic[i] === "" && heading.symbol) {
|
|
65
|
+
anchor = "section";
|
|
66
|
+
for (let n = 1; ids.has(anchor); n++) anchor = `section-${n}`;
|
|
67
|
+
ids.add(anchor);
|
|
68
|
+
}
|
|
69
|
+
heading.anchor.value = anchor;
|
|
70
|
+
if (!heading.symbol) return;
|
|
71
|
+
const name = anchor ?? automatic[i];
|
|
72
|
+
if (name === undefined) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
"A Heading with a refkey and non-string children requires an explicit id.",
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
heading.symbol.name = name;
|
|
78
|
+
});
|
|
79
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { createContext, useContext } from "@alloy-js/core";
|
|
2
|
+
|
|
3
|
+
/** Whether inline Markdown is being rendered inside a table cell. */
|
|
4
|
+
export const TableContext = createContext<boolean>(false);
|
|
5
|
+
|
|
6
|
+
export function useTableContext(): boolean {
|
|
7
|
+
return useContext(TableContext) === true;
|
|
8
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createSymbol,
|
|
3
|
+
OutputSymbol,
|
|
4
|
+
type OutputSpace,
|
|
5
|
+
type OutputSymbolOptions,
|
|
6
|
+
} from "@alloy-js/core";
|
|
7
|
+
|
|
8
|
+
export interface HeadingSymbolOptions extends OutputSymbolOptions {
|
|
9
|
+
path: string;
|
|
10
|
+
text: () => string | undefined;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class HeadingSymbol extends OutputSymbol {
|
|
14
|
+
/** Path of the file declaring the heading. */
|
|
15
|
+
readonly path: string;
|
|
16
|
+
/** Trimmed heading text, or `undefined` when the heading has JSX children. */
|
|
17
|
+
readonly text: () => string | undefined;
|
|
18
|
+
|
|
19
|
+
constructor(
|
|
20
|
+
name: string,
|
|
21
|
+
spaces: OutputSpace | OutputSpace[] | undefined,
|
|
22
|
+
options: HeadingSymbolOptions,
|
|
23
|
+
) {
|
|
24
|
+
super(name, spaces, options);
|
|
25
|
+
this.path = options.path;
|
|
26
|
+
this.text = options.text;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
copy() {
|
|
30
|
+
const copy = createSymbol(HeadingSymbol, this.name, undefined, {
|
|
31
|
+
...this.getCopyOptions(),
|
|
32
|
+
path: this.path,
|
|
33
|
+
text: this.text,
|
|
34
|
+
});
|
|
35
|
+
this.initializeCopy(copy);
|
|
36
|
+
return copy;
|
|
37
|
+
}
|
|
38
|
+
}
|
package/test/Code.test.tsx
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import { List } from "@alloy-js/core";
|
|
2
|
-
import {
|
|
1
|
+
import { List, ref } from "@alloy-js/core";
|
|
2
|
+
import {
|
|
3
|
+
d,
|
|
4
|
+
printTree,
|
|
5
|
+
renderToString,
|
|
6
|
+
renderTree,
|
|
7
|
+
} from "@alloy-js/core/testing";
|
|
8
|
+
import { marked } from "marked";
|
|
3
9
|
import { expect, it } from "vitest";
|
|
4
10
|
|
|
5
11
|
import { Code } from "../src/components/Code.jsx";
|
|
@@ -35,3 +41,97 @@ it("renders with children", () => {
|
|
|
35
41
|
\`\`\`
|
|
36
42
|
`);
|
|
37
43
|
});
|
|
44
|
+
|
|
45
|
+
it("omits an absent language instead of emitting undefined", () => {
|
|
46
|
+
const output = renderToString(<Code>plain text</Code>);
|
|
47
|
+
expect(output).toBe("```\nplain text\n```");
|
|
48
|
+
const tokens = marked.lexer(output);
|
|
49
|
+
expect(tokens).toHaveLength(1);
|
|
50
|
+
expect(tokens[0]).toMatchObject({
|
|
51
|
+
type: "code",
|
|
52
|
+
lang: "",
|
|
53
|
+
text: "plain text",
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it.each(["before\n```\nafter", "before\n``````\nafter"])(
|
|
58
|
+
"keeps conflicting fences in a single parsed code block: %s",
|
|
59
|
+
(body) => {
|
|
60
|
+
const output = renderToString(<Code lang="md">{body}</Code>);
|
|
61
|
+
const tokens = marked.lexer(output);
|
|
62
|
+
expect(tokens).toHaveLength(1);
|
|
63
|
+
expect(tokens[0]).toMatchObject({ type: "code", lang: "md", text: body });
|
|
64
|
+
},
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
it("updates fences when a direct string body changes", () => {
|
|
68
|
+
const body = ref("before\n```\nafter");
|
|
69
|
+
const tree = renderTree(<Code lang="md">{body.value}</Code>);
|
|
70
|
+
expect(printTree(tree)).toBe("````md\nbefore\n```\nafter\n````");
|
|
71
|
+
body.value = "before\n``````\nafter";
|
|
72
|
+
const tokens = marked.lexer(printTree(tree));
|
|
73
|
+
expect(tokens).toHaveLength(1);
|
|
74
|
+
expect(tokens[0]).toMatchObject({
|
|
75
|
+
type: "code",
|
|
76
|
+
lang: "md",
|
|
77
|
+
text: body.value,
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("leaves JSX children unchanged without inspecting them for fence conflicts", () => {
|
|
82
|
+
const run = ref("``");
|
|
83
|
+
let renders = 0;
|
|
84
|
+
function Body() {
|
|
85
|
+
renders++;
|
|
86
|
+
return (
|
|
87
|
+
<List>
|
|
88
|
+
before
|
|
89
|
+
<>{run}`</>
|
|
90
|
+
after
|
|
91
|
+
</List>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
const tree = renderTree(
|
|
95
|
+
<Code lang="md">
|
|
96
|
+
<group>
|
|
97
|
+
<Body />
|
|
98
|
+
</group>
|
|
99
|
+
</Code>,
|
|
100
|
+
);
|
|
101
|
+
expect(printTree(tree)).toBe("```md\nbefore\n```\nafter\n```");
|
|
102
|
+
run.value = "`````";
|
|
103
|
+
const output = printTree(tree);
|
|
104
|
+
expect(output).toBe("```md\nbefore\n``````\nafter\n```");
|
|
105
|
+
expect(renders).toBe(1);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("preserves info strings containing backticks with safe tilde fences", () => {
|
|
109
|
+
const body = "before\n~~~\nafter\n";
|
|
110
|
+
const output = renderToString(
|
|
111
|
+
<Code lang={' md title="`example`" '}>{body}</Code>,
|
|
112
|
+
);
|
|
113
|
+
expect(output).toBe('~~~~md title="`example`"\nbefore\n~~~\nafter\n\n~~~~');
|
|
114
|
+
const tokens = marked.lexer(output);
|
|
115
|
+
expect(tokens).toHaveLength(1);
|
|
116
|
+
expect(tokens[0]).toMatchObject({
|
|
117
|
+
type: "code",
|
|
118
|
+
lang: 'md title="`example`"',
|
|
119
|
+
text: body,
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("preserves trailing blank lines in string bodies", () => {
|
|
124
|
+
const body = "first\nsecond\n\n";
|
|
125
|
+
const tokens = marked.lexer(renderToString(<Code>{body}</Code>));
|
|
126
|
+
expect(tokens).toHaveLength(1);
|
|
127
|
+
expect(tokens[0]).toMatchObject({ type: "code", text: body });
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it.each(["md\ninjected", "md\r", "\nmd"])(
|
|
131
|
+
"rejects multiline info strings: %j",
|
|
132
|
+
(lang) => {
|
|
133
|
+
expect(() => renderToString(<Code lang={lang}>body</Code>)).toThrow(
|
|
134
|
+
"Code lang must be a single-line Markdown info string.",
|
|
135
|
+
);
|
|
136
|
+
},
|
|
137
|
+
);
|