igniteui-astro-components 0.0.30 → 1.0.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/package.json +18 -15
- package/src/integration.ts +26 -14
- package/src/plugins/rehype-heading-anchors.ts +53 -61
- package/src/plugins/rehype-pagefind-ignore.ts +19 -28
- package/src/plugins/rehype-table-wrapper.ts +38 -36
- package/src/plugins/remark-env-vars.ts +26 -20
- package/src/plugins/remark-html-transforms.ts +21 -18
- package/src/plugins/remark-md-links.ts +43 -31
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "igniteui-astro-components",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Reusable Astro components, layouts, and integrations for Infragistics / Ignite UI documentation sites. Replaces the Starlight defaults with project-specific implementations.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": "
|
|
7
|
+
"node": ">=22.12.0"
|
|
8
8
|
},
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"author": "Infragistics",
|
|
@@ -83,41 +83,44 @@
|
|
|
83
83
|
"./package.json": "./package.json"
|
|
84
84
|
},
|
|
85
85
|
"peerDependencies": {
|
|
86
|
-
"astro": "^
|
|
86
|
+
"astro": "^7.0.0",
|
|
87
|
+
"igniteui-theming": "^26.0.1",
|
|
87
88
|
"igniteui-webcomponents": "^7.2.4"
|
|
88
89
|
},
|
|
89
90
|
"dependencies": {
|
|
91
|
+
"@astrojs/mdx": "^7.0.7",
|
|
92
|
+
"@astrojs/markdown-satteri": "^0.3.5",
|
|
90
93
|
"@fontsource-variable/instrument-sans": "^5.2.8",
|
|
91
94
|
"@fontsource-variable/inter": "^5.2.8",
|
|
92
95
|
"@fontsource-variable/jetbrains-mono": "^5.2.8",
|
|
96
|
+
"github-slugger": "^2.0.0",
|
|
93
97
|
"jsdom": "^29.0.1",
|
|
94
|
-
"
|
|
98
|
+
"satteri": "^0.9.5",
|
|
99
|
+
"shiki": "^4.3.0"
|
|
95
100
|
},
|
|
96
101
|
"devDependencies": {
|
|
102
|
+
"@astrojs/check": "^0.9.9",
|
|
97
103
|
"@types/jsdom": "^21.1.7",
|
|
98
|
-
"astro": "^
|
|
104
|
+
"astro": "^7.2.2",
|
|
99
105
|
"husky": "^9.1.7",
|
|
100
106
|
"igniteui-theming": "^26.0.1",
|
|
107
|
+
"igniteui-webcomponents": "^7.3.0-beta.1",
|
|
101
108
|
"lint-staged": "^17.0.8",
|
|
102
109
|
"oxlint": "^1.72.0",
|
|
103
110
|
"prettier": "^3.9.4",
|
|
104
111
|
"prettier-plugin-astro": "^0.14.1",
|
|
105
112
|
"sass": "^1.83.0",
|
|
106
|
-
"typescript": "^5.
|
|
113
|
+
"typescript": "^5.9.3"
|
|
107
114
|
},
|
|
108
|
-
"workspaces": [
|
|
109
|
-
"playground"
|
|
110
|
-
],
|
|
111
115
|
"publishConfig": {
|
|
112
116
|
"access": "public"
|
|
113
117
|
},
|
|
114
118
|
"scripts": {
|
|
115
|
-
"playground": "npm
|
|
116
|
-
"playground:dev": "
|
|
117
|
-
"playground:build": "
|
|
118
|
-
"playground:preview": "
|
|
119
|
-
"playground:
|
|
120
|
-
"playground:check": "npm --prefix playground run check",
|
|
119
|
+
"playground": "npm run playground:dev",
|
|
120
|
+
"playground:dev": "astro sync --root playground && astro dev --root playground --port 4567",
|
|
121
|
+
"playground:build": "astro sync --root playground && astro build --root playground",
|
|
122
|
+
"playground:preview": "astro preview --root playground --port 4567",
|
|
123
|
+
"playground:check": "astro check --root playground",
|
|
121
124
|
"lint": "oxlint .",
|
|
122
125
|
"lint:fix": "oxlint . --fix",
|
|
123
126
|
"format": "prettier --write .",
|
package/src/integration.ts
CHANGED
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
* { tag: 'link', attrs: { rel: 'stylesheet', href: '...' } },
|
|
26
26
|
* ],
|
|
27
27
|
* // Extra Astro options (markdown, image, build, …)
|
|
28
|
-
* markdown: {
|
|
28
|
+
* markdown: { mdastPlugins: [], hastPlugins: [] },
|
|
29
29
|
* });
|
|
30
30
|
*
|
|
31
31
|
* --- Option B: manual composition (for full control) ---
|
|
@@ -66,6 +66,7 @@ import {
|
|
|
66
66
|
import { buildSidebarFromToc } from './sidebar';
|
|
67
67
|
import { getPlatformHead } from './platform';
|
|
68
68
|
import type { HeadEntry, PlatformKey, NavLang } from './platform.ts';
|
|
69
|
+
import { satteri } from '@astrojs/markdown-satteri';
|
|
69
70
|
import { remarkEnvVars } from './plugins/remark-env-vars';
|
|
70
71
|
export { rehypeTableWrapper } from './plugins/rehype-table-wrapper';
|
|
71
72
|
import { rehypeTableWrapper } from './plugins/rehype-table-wrapper';
|
|
@@ -699,6 +700,13 @@ export function createDocsSite(
|
|
|
699
700
|
return defineConfig({
|
|
700
701
|
site,
|
|
701
702
|
...(base !== undefined ? { base } : {}),
|
|
703
|
+
// Astro 7 defaults to `compressHTML: 'jsx'`, which drops whitespace between
|
|
704
|
+
// inline elements entirely instead of collapsing it to a single space. That
|
|
705
|
+
// silently joins adjacent text — breadcrumb segments, badge labels and callout
|
|
706
|
+
// titles render as "Open SourceIgnite UI" / "InfoThe -y flag". Keeping the
|
|
707
|
+
// Astro 6 behaviour preserves the rendered copy; switch to 'jsx' only after
|
|
708
|
+
// auditing templates for the inline pairs that need an explicit {" "}.
|
|
709
|
+
compressHTML: true,
|
|
702
710
|
// Docs sites serve images statically — disable Astro's image optimization
|
|
703
711
|
// so relative image paths in markdown don't cause build errors.
|
|
704
712
|
image: { service: { entrypoint: 'astro/assets/services/noop' }, ...(astroExtra as any).image },
|
|
@@ -715,19 +723,23 @@ export function createDocsSite(
|
|
|
715
723
|
},
|
|
716
724
|
markdown: {
|
|
717
725
|
...(astroExtra as any).markdown,
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
726
|
+
// Astro 7 renders Markdown/MDX with Sätteri instead of remark/rehype.
|
|
727
|
+
// MDAST plugins run before HAST plugins, matching the old
|
|
728
|
+
// remarkPlugins-then-rehypePlugins ordering.
|
|
729
|
+
processor: satteri({
|
|
730
|
+
mdastPlugins: [
|
|
731
|
+
remarkEnvVars,
|
|
732
|
+
remarkMdLinks,
|
|
733
|
+
remarkHtmlTransforms,
|
|
734
|
+
...((astroExtra as any).markdown?.mdastPlugins ?? []),
|
|
735
|
+
],
|
|
736
|
+
hastPlugins: [
|
|
737
|
+
rehypeTableWrapper,
|
|
738
|
+
rehypeHeadingAnchors,
|
|
739
|
+
rehypePagefindIgnore,
|
|
740
|
+
...((astroExtra as any).markdown?.hastPlugins ?? []),
|
|
741
|
+
],
|
|
742
|
+
}),
|
|
731
743
|
},
|
|
732
744
|
integrations: [
|
|
733
745
|
siteMetaIntegration({
|
|
@@ -1,74 +1,66 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { defineHastPlugin } from 'satteri';
|
|
2
2
|
import Slugger from 'github-slugger';
|
|
3
3
|
|
|
4
|
-
const HEADING_TAGS =
|
|
5
|
-
|
|
6
|
-
// Extract the text content of a hast node (ignores child elements, reads text/raw nodes).
|
|
7
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
8
|
-
function textContent(node: any): string {
|
|
9
|
-
let text = '';
|
|
10
|
-
visit(node, (child) => {
|
|
11
|
-
if (child.type === 'text' || child.type === 'raw') text += child.value ?? '';
|
|
12
|
-
});
|
|
13
|
-
return text;
|
|
14
|
-
}
|
|
4
|
+
const HEADING_TAGS = ['h2', 'h3', 'h4', 'h5', 'h6'];
|
|
15
5
|
|
|
16
6
|
/**
|
|
17
|
-
*
|
|
7
|
+
* Sätteri HAST plugin that appends a copy-link icon button to every heading
|
|
18
8
|
* (h2–h6).
|
|
19
9
|
*
|
|
20
|
-
* Runs before Astro's internal
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
10
|
+
* Runs before Astro's internal heading-id pass so it must generate its own slug
|
|
11
|
+
* using the same `github-slugger` algorithm. If the heading already has an `id`
|
|
12
|
+
* (e.g. set in raw HTML), it is reused. Headings without an existing id get one
|
|
13
|
+
* assigned here — the built-in pass will then see the id already present and
|
|
14
|
+
* skip re-assigning it, ensuring the button href and the heading's id always
|
|
15
|
+
* match.
|
|
16
|
+
*
|
|
17
|
+
* Exported as a factory so Sätteri calls it once per document, giving each page
|
|
18
|
+
* a fresh slugger and therefore a fresh duplicate-suffix counter.
|
|
26
19
|
*/
|
|
27
20
|
export function rehypeHeadingAnchors() {
|
|
28
|
-
|
|
29
|
-
return (tree: any) => {
|
|
30
|
-
const slugger = new Slugger();
|
|
31
|
-
|
|
32
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
|
-
visit(tree, 'element', (node: any) => {
|
|
34
|
-
if (!HEADING_TAGS.has(node.tagName)) return;
|
|
21
|
+
const slugger = new Slugger();
|
|
35
22
|
|
|
36
|
-
|
|
23
|
+
return defineHastPlugin({
|
|
24
|
+
name: 'heading-anchors',
|
|
25
|
+
element: {
|
|
26
|
+
filter: HEADING_TAGS,
|
|
27
|
+
visit(node, ctx) {
|
|
28
|
+
// Reuse an existing id or generate one from heading text.
|
|
29
|
+
const existing = node.properties?.id;
|
|
30
|
+
let id: string;
|
|
37
31
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
32
|
+
if (typeof existing === 'string' && existing) {
|
|
33
|
+
// Keep slugger in sync so it doesn't generate duplicate ids.
|
|
34
|
+
slugger.slug(existing);
|
|
35
|
+
id = existing;
|
|
36
|
+
} else {
|
|
37
|
+
id = slugger.slug(ctx.textContent(node));
|
|
38
|
+
ctx.setProperty(node, 'id', id);
|
|
39
|
+
}
|
|
45
40
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
'aria-label': 'Link to this section',
|
|
56
|
-
tabIndex: 0,
|
|
57
|
-
},
|
|
58
|
-
children: [
|
|
59
|
-
{
|
|
60
|
-
type: 'element',
|
|
61
|
-
tagName: 'igc-icon',
|
|
62
|
-
properties: {
|
|
63
|
-
name: 'link',
|
|
64
|
-
collection: 'docs',
|
|
65
|
-
},
|
|
66
|
-
children: [],
|
|
41
|
+
ctx.appendChild(node, {
|
|
42
|
+
type: 'element',
|
|
43
|
+
tagName: 'igc-icon-button',
|
|
44
|
+
properties: {
|
|
45
|
+
class: 'igd-anchor-link',
|
|
46
|
+
href: `#${id}`,
|
|
47
|
+
variant: 'flat',
|
|
48
|
+
'aria-label': 'Link to this section',
|
|
49
|
+
tabIndex: 0,
|
|
67
50
|
},
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
51
|
+
children: [
|
|
52
|
+
{
|
|
53
|
+
type: 'element',
|
|
54
|
+
tagName: 'igc-icon',
|
|
55
|
+
properties: {
|
|
56
|
+
name: 'link',
|
|
57
|
+
collection: 'docs',
|
|
58
|
+
},
|
|
59
|
+
children: [],
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
});
|
|
74
66
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { defineHastPlugin } from 'satteri';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Marks code with `data-pagefind-ignore` so Pagefind doesn't index it.
|
|
@@ -8,33 +8,24 @@ import { visit } from 'unist-util-visit';
|
|
|
8
8
|
* `keyof T` across unrelated pages. Excluding `<pre>` (and single-char inline
|
|
9
9
|
* `<code>`) removes that noise; real inline `<code>` like `dataSource` stays.
|
|
10
10
|
*/
|
|
11
|
-
|
|
12
|
-
// Concatenate all descendant text of a HAST node.
|
|
13
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
-
function textContent(node: any): string {
|
|
15
|
-
if (node.type === 'text') return node.value ?? '';
|
|
16
|
-
if (!node.children) return '';
|
|
17
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
-
return node.children.map((c: any) => textContent(c)).join('');
|
|
19
|
-
}
|
|
20
|
-
|
|
21
11
|
export function rehypePagefindIgnore() {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
12
|
+
return defineHastPlugin({
|
|
13
|
+
name: 'pagefind-ignore',
|
|
14
|
+
element: {
|
|
15
|
+
filter: ['pre', 'code'],
|
|
16
|
+
visit(node, ctx) {
|
|
17
|
+
// Fenced code blocks (covers their inner <code> via subtree ignore).
|
|
18
|
+
if (node.tagName === 'pre') {
|
|
19
|
+
ctx.setProperty(node, 'data-pagefind-ignore', 'all');
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
32
22
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
23
|
+
// Inline code that reduces to a single alphanumeric token.
|
|
24
|
+
const token = ctx.textContent(node).replace(/[^a-z0-9]/gi, '');
|
|
25
|
+
if (token.length <= 1) {
|
|
26
|
+
ctx.setProperty(node, 'data-pagefind-ignore', 'all');
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
});
|
|
40
31
|
}
|
|
@@ -1,49 +1,51 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
let _counter = 0;
|
|
1
|
+
import { defineHastPlugin } from 'satteri';
|
|
4
2
|
|
|
5
3
|
/**
|
|
6
|
-
*
|
|
4
|
+
* Sätteri HAST plugin that wraps every <table> in a scroll container div.
|
|
7
5
|
* The wrapper is fully accessible per the recommendations in:
|
|
8
6
|
* https://piccalil.li/blog/styling-tables-the-modern-css-way/
|
|
9
7
|
*
|
|
10
8
|
* - role="region" + aria-labelledby pointing to the <caption> (or a generated id)
|
|
11
9
|
* - tabindex="0" makes the scroll region keyboard-focusable
|
|
10
|
+
*
|
|
11
|
+
* Exported as a factory so Sätteri calls it once per document, resetting the id
|
|
12
|
+
* counter — these ids only have to be unique within a single page.
|
|
12
13
|
*/
|
|
13
14
|
export function rehypeTableWrapper() {
|
|
14
|
-
|
|
15
|
-
return (tree: any) => {
|
|
16
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
17
|
-
visit(tree, 'element', (node: any, index: number | undefined, parent: any) => {
|
|
18
|
-
if (node.tagName !== 'table' || !parent || index == null) return;
|
|
19
|
-
|
|
20
|
-
// Find an existing <caption> to use as the accessible label.
|
|
21
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
-
const caption = node.children?.find((c: any) => c.tagName === 'caption');
|
|
23
|
-
let labelId: string;
|
|
15
|
+
let counter = 0;
|
|
24
16
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
//
|
|
31
|
-
|
|
32
|
-
|
|
17
|
+
return defineHastPlugin({
|
|
18
|
+
name: 'table-wrapper',
|
|
19
|
+
element: {
|
|
20
|
+
filter: ['table'],
|
|
21
|
+
visit(node, ctx) {
|
|
22
|
+
// Find an existing <caption> to use as the accessible label.
|
|
23
|
+
const caption = node.children?.find((c) => c.type === 'element' && c.tagName === 'caption');
|
|
24
|
+
let labelId: string;
|
|
33
25
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
};
|
|
26
|
+
if (caption && caption.type === 'element') {
|
|
27
|
+
// Reuse or assign an id on the caption element.
|
|
28
|
+
const existing = caption.properties?.id;
|
|
29
|
+
labelId =
|
|
30
|
+
typeof existing === 'string' && existing ? existing : `igd-table-caption-${++counter}`;
|
|
31
|
+
ctx.setProperty(caption, 'id', labelId);
|
|
32
|
+
} else {
|
|
33
|
+
// No caption — generate a unique id for the wrapper itself.
|
|
34
|
+
labelId = `igd-table-${++counter}`;
|
|
35
|
+
}
|
|
45
36
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
37
|
+
ctx.wrapNode(node, {
|
|
38
|
+
type: 'element',
|
|
39
|
+
tagName: 'div',
|
|
40
|
+
properties: {
|
|
41
|
+
className: ['igd-table-wrapper'],
|
|
42
|
+
role: 'region',
|
|
43
|
+
'aria-labelledby': labelId,
|
|
44
|
+
tabIndex: 0,
|
|
45
|
+
},
|
|
46
|
+
children: [],
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
});
|
|
49
51
|
}
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* DOCS_ENV (or NODE_ENV) selects the env block: development | staging | production.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import {
|
|
17
|
+
import { defineMdastPlugin } from 'satteri';
|
|
18
18
|
import fs from 'node:fs';
|
|
19
19
|
import path from 'node:path';
|
|
20
20
|
|
|
@@ -95,24 +95,30 @@ export function replaceEnvVars(str: string): string {
|
|
|
95
95
|
return str.replace(ENV_PATTERN, (_match, key) => env[key] ?? `{environment:${key}}`);
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
/**
|
|
98
|
+
/**
|
|
99
|
+
* Sätteri MDAST plugin that substitutes `{environment:key}` tokens.
|
|
100
|
+
*
|
|
101
|
+
* Exported as a factory so Sätteri calls it once per compile; the `loadEnv()`
|
|
102
|
+
* cache is module-level and intentionally shared across documents.
|
|
103
|
+
*/
|
|
99
104
|
export function remarkEnvVars() {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
if (node.
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
105
|
+
return defineMdastPlugin({
|
|
106
|
+
name: 'env-vars',
|
|
107
|
+
text(node, ctx) {
|
|
108
|
+
const value = replaceEnvVars(node.value);
|
|
109
|
+
if (value !== node.value) ctx.setProperty(node, 'value', value);
|
|
110
|
+
},
|
|
111
|
+
html(node, ctx) {
|
|
112
|
+
const value = replaceEnvVars(node.value);
|
|
113
|
+
if (value !== node.value) ctx.setProperty(node, 'value', value);
|
|
114
|
+
},
|
|
115
|
+
link(node, ctx) {
|
|
116
|
+
const url = replaceEnvVars(node.url);
|
|
117
|
+
if (url !== node.url) ctx.setProperty(node, 'url', url);
|
|
118
|
+
},
|
|
119
|
+
image(node, ctx) {
|
|
120
|
+
const url = replaceEnvVars(node.url);
|
|
121
|
+
if (url !== node.url) ctx.setProperty(node, 'url', url);
|
|
122
|
+
},
|
|
123
|
+
});
|
|
118
124
|
}
|
|
@@ -1,32 +1,35 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Sätteri MDAST plugin: inline HTML transforms.
|
|
3
3
|
*
|
|
4
4
|
* Handles legacy HTML patterns in markdown content:
|
|
5
5
|
* - `<div class="divider--half"></div>` → `<hr/>`
|
|
6
6
|
* - Normalizes code block language identifiers to lowercase
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {
|
|
9
|
+
import { defineMdastPlugin } from 'satteri';
|
|
10
10
|
|
|
11
11
|
const DIVIDER_PATTERN = /<div\s+class="divider--half"\s*>\s*<\/div>/g;
|
|
12
12
|
const IMG_SRC_PATTERN = /src="(\.\.\/)+images\//g;
|
|
13
13
|
|
|
14
|
-
/**
|
|
14
|
+
/** Sätteri MDAST plugin that transforms legacy HTML patterns in the AST. */
|
|
15
15
|
export function remarkHtmlTransforms() {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
-
visit(tree, (node: any) => {
|
|
20
|
-
// Inline HTML: divider → hr, relative img src → root-relative
|
|
21
|
-
if (node.type === 'html' && node.value) {
|
|
22
|
-
node.value = (node.value as string).replace(DIVIDER_PATTERN, '<hr/>');
|
|
23
|
-
node.value = (node.value as string).replace(IMG_SRC_PATTERN, 'src="/images/');
|
|
24
|
-
}
|
|
16
|
+
return defineMdastPlugin({
|
|
17
|
+
name: 'html-transforms',
|
|
25
18
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
19
|
+
// Inline HTML: divider → hr, relative img src → root-relative
|
|
20
|
+
html(node, ctx) {
|
|
21
|
+
if (!node.value) return;
|
|
22
|
+
const value = node.value
|
|
23
|
+
.replace(DIVIDER_PATTERN, '<hr/>')
|
|
24
|
+
.replace(IMG_SRC_PATTERN, 'src="/images/');
|
|
25
|
+
if (value !== node.value) ctx.setProperty(node, 'value', value);
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
// Code blocks: normalize language to lowercase
|
|
29
|
+
code(node, ctx) {
|
|
30
|
+
if (!node.lang) return;
|
|
31
|
+
const lang = node.lang.toLowerCase();
|
|
32
|
+
if (lang !== node.lang) ctx.setProperty(node, 'lang', lang);
|
|
33
|
+
},
|
|
34
|
+
});
|
|
32
35
|
}
|
|
@@ -8,8 +8,9 @@
|
|
|
8
8
|
* that are already absolute but missing the site base path.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
import {
|
|
11
|
+
import { defineMdastPlugin } from 'satteri';
|
|
12
12
|
import path from 'node:path';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
16
|
* Resolve a relative .md link to an absolute Astro URL.
|
|
@@ -43,39 +44,50 @@ function rewriteMdLink(url: string, filePath: string, docsDir: string): string {
|
|
|
43
44
|
return docsBase + '/' + slug.toLowerCase() + '/' + suffix;
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
/**
|
|
47
|
+
/** Resolve the source file path and docs root for the document being compiled. */
|
|
48
|
+
function resolvePaths(fileURL: URL | undefined): { filePath: string; docsDir: string } {
|
|
49
|
+
const filePath = fileURL ? fileURLToPath(fileURL) : '';
|
|
50
|
+
const docsDir = process.env.DOCS_SOURCE_PATH
|
|
51
|
+
? path.resolve(process.env.DOCS_SOURCE_PATH)
|
|
52
|
+
: filePath
|
|
53
|
+
? path.dirname(filePath)
|
|
54
|
+
: '';
|
|
55
|
+
return { filePath, docsDir };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Satteri MDAST plugin that rewrites relative .md links, prepends DOCS_BASE,
|
|
60
|
+
* and fixes relative image paths.
|
|
61
|
+
*/
|
|
47
62
|
export function remarkMdLinks() {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const filePath = (file.path as string) ?? '';
|
|
51
|
-
const docsDir = process.env.DOCS_SOURCE_PATH
|
|
52
|
-
? path.resolve(process.env.DOCS_SOURCE_PATH)
|
|
53
|
-
: filePath
|
|
54
|
-
? path.dirname(filePath)
|
|
55
|
-
: '';
|
|
63
|
+
return defineMdastPlugin({
|
|
64
|
+
name: 'md-links',
|
|
56
65
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
66
|
+
link(node, ctx) {
|
|
67
|
+
if (!node.url) return;
|
|
68
|
+
const { filePath, docsDir } = resolvePaths(ctx.fileURL);
|
|
69
|
+
let url = rewriteMdLink(node.url, filePath, docsDir);
|
|
61
70
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
71
|
+
// Prepend DOCS_BASE to root-relative internal links not already prefixed.
|
|
72
|
+
const docsBase = (process.env.DOCS_BASE ?? '').replace(/[/]$/, '');
|
|
73
|
+
if (
|
|
74
|
+
docsBase &&
|
|
75
|
+
url.startsWith('/') &&
|
|
76
|
+
!url.startsWith('//') &&
|
|
77
|
+
!url.startsWith(docsBase + '/')
|
|
78
|
+
) {
|
|
79
|
+
url = docsBase + url;
|
|
72
80
|
}
|
|
73
81
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
82
|
+
if (url !== node.url) ctx.setProperty(node, 'url', url);
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
// Rewrite relative `../images/` paths in markdown image nodes to root-relative `/images/`.
|
|
86
|
+
// Generated MDX files may contain relative image references that Vite cannot resolve.
|
|
87
|
+
image(node, ctx) {
|
|
88
|
+
if (!node.url) return;
|
|
89
|
+
const url = node.url.replace(/^([.][.][/])+images[/]/, '/images/');
|
|
90
|
+
if (url !== node.url) ctx.setProperty(node, 'url', url);
|
|
91
|
+
},
|
|
92
|
+
});
|
|
81
93
|
}
|