@alloy-js/markdown 0.26.0 → 0.27.0-dev.4
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 +11 -6
- 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/temp/api.json +552 -9
- 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
|
@@ -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
|
+
}
|