Skip to content

Commit e58d23c

Browse files
delucisbholmesdev
andauthored
Migrate docs to MDX (en-only) (withastro#2133)
Co-authored-by: bholmesdev <[email protected]>
1 parent 1bef735 commit e58d23c

File tree

473 files changed

+2821
-2411
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

473 files changed

+2821
-2411
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ indent_style = tab
1111
insert_final_newline = true
1212
trim_trailing_whitespace = false
1313

14-
[{.*,*.md,*.json,*.toml,*.yml,}]
14+
[{.*,*.md,*.mdx,*.json,*.toml,*.yml,}]
1515
indent_style = space

.github/labeler.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
- .github/workflows/**
55

66
i18n:
7-
- any: ['src/pages/**/*.md', '!src/pages/en/**/*']
7+
- any: ['src/pages/**/*.mdx', '!src/pages/en/**/*']

.github/workflows/ci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ concurrency:
1111
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request_target' && github.head_ref || github.ref }}
1212
cancel-in-progress: true
1313

14+
env:
15+
NODE_OPTIONS: "--max_old_space_size=4096"
16+
1417
jobs:
1518
eslint:
1619
name: Check for code issues with ESLint

.github/workflows/nightly.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
with:
2929
branch: ci/docgen-integrations
3030
token: ${{ secrets.FREDKBOT_GITHUB_TOKEN }}
31-
add-paths: src/pages/en/guides/integrations-guide/*.md
31+
add-paths: src/pages/en/guides/integrations-guide/*.mdx
3232
commit-message: 'ci: update integration docs'
3333
title: 'ci: update integration docs'
3434
body: |
@@ -55,7 +55,7 @@ jobs:
5555
with:
5656
branch: ci/docgen
5757
token: ${{ secrets.FREDKBOT_GITHUB_TOKEN }}
58-
add-paths: src/pages/en/reference/*.md
58+
add-paths: src/pages/en/reference/*.mdx
5959
commit-message: 'ci: update reference docs'
6060
title: 'ci: update reference docs'
6161
body: |
@@ -82,7 +82,7 @@ jobs:
8282
with:
8383
branch: ci/docgen-errors
8484
token: ${{ secrets.FREDKBOT_GITHUB_TOKEN }}
85-
add-paths: src/pages/en/reference/*.md
85+
add-paths: src/pages/en/reference/*.mdx
8686
commit-message: 'ci: update error reference docs'
8787
title: 'ci: update error reference docs'
8888
body: |

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ node_modules
33
.github
44
.changeset
55
*.md
6+
*.mdx
67
src/pages/lighthouse/*.astro
78
pnpm-lock.yaml

.prettierrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"useTabs": true,
88
"overrides": [
99
{
10-
"files": [".*", "*.json", "*.md", "*.toml", "*.yml"],
10+
"files": [".*", "*.json", "*.md", "*.mdx", "*.toml", "*.yml"],
1111
"options": {
1212
"useTabs": false
1313
}

astro.config.ts

+20-70
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,51 @@
1+
import mdx from '@astrojs/mdx';
12
import preact from '@astrojs/preact';
23
import { defineConfig } from 'astro/config';
34

4-
import { toString } from 'hast-util-to-string';
5-
import { h } from 'hastscript';
6-
import { escape } from 'html-escaper';
5+
import AutoImport from 'astro-auto-import';
6+
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
7+
import rehypeSlug from 'rehype-slug';
8+
import remarkGFM from 'remark-gfm';
9+
import remarkSmartypants from 'remark-smartypants';
710

8-
import { astroAsides } from './integrations/astro-asides';
9-
import { astroCodeSnippets } from './integrations/astro-code-snippets';
10-
import { astroSpoilers } from './integrations/astro-spoilers';
11+
import { asideAutoImport, astroAsides } from './integrations/astro-asides';
12+
import { astroCodeSnippets, codeSnippetAutoImport } from './integrations/astro-code-snippets';
13+
import { astroSpoilers, spoilerAutoImport } from './integrations/astro-spoilers';
1114
import { sitemap } from './integrations/sitemap';
15+
import { autolinkConfig } from './plugins/rehype-autolink-config';
1216
import { rehypei18nAutolinkHeadings } from './plugins/rehype-i18n-autolink-headings';
1317
import { rehypeTasklistEnhancer } from './plugins/rehype-tasklist-enhancer';
1418
import { remarkFallbackLang } from './plugins/remark-fallback-lang';
15-
import { backgroundPrimary, foregroundPrimary, tokens } from './syntax-highlighting-theme';
16-
17-
import { useTranslationsForLang } from './src/i18n/util';
18-
19-
const AnchorLinkIcon = h(
20-
'svg',
21-
{
22-
width: 16,
23-
height: 16,
24-
version: 1.1,
25-
viewBox: '0 0 16 16',
26-
xlmns: 'http://www.w3.org/2000/svg',
27-
},
28-
h('path', {
29-
fillRule: 'evenodd',
30-
fill: 'currentcolor',
31-
d: 'M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z',
32-
})
33-
);
34-
35-
const createSROnlyLabel = (text: string) => {
36-
const t = useTranslationsForLang('en');
37-
const node = h('span.sr-only', `${t('a11y.sectionLink')} ${escape(text)}`);
38-
node.properties!['is:raw'] = true;
39-
return node;
40-
};
19+
import { theme } from './syntax-highlighting-theme';
4120

4221
// https://astro.build/config
4322
export default defineConfig({
4423
site: 'https://docs.astro.build/',
45-
legacy: {
46-
astroFlavoredMarkdown: true,
47-
},
4824
integrations: [
25+
AutoImport({
26+
imports: [asideAutoImport, codeSnippetAutoImport, spoilerAutoImport],
27+
}),
4928
preact({ compat: true }),
5029
sitemap(),
5130
astroAsides(),
5231
astroSpoilers(),
5332
astroCodeSnippets(),
33+
mdx(),
5434
],
5535
markdown: {
5636
syntaxHighlight: 'shiki',
57-
shikiConfig: {
58-
theme: {
59-
name: 'Star gazer',
60-
type: 'dark',
61-
settings: tokens,
62-
fg: foregroundPrimary,
63-
bg: backgroundPrimary,
64-
},
65-
},
37+
shikiConfig: { theme },
6638
remarkPlugins: [
6739
// These are here because setting custom plugins disables the default plugins
68-
'remark-gfm',
69-
['remark-smartypants', { dashes: false }],
40+
remarkGFM,
41+
[remarkSmartypants, { dashes: false }],
7042
// Add our custom plugin that marks links to fallback language pages
7143
remarkFallbackLang(),
7244
],
7345
rehypePlugins: [
74-
'rehype-slug',
46+
rehypeSlug,
7547
// This adds links to headings
76-
[
77-
'rehype-autolink-headings',
78-
{
79-
properties: {
80-
class: 'anchor-link',
81-
},
82-
behavior: 'after',
83-
group: ({ tagName }) =>
84-
h(`div.heading-wrapper.level-${tagName}`, {
85-
tabIndex: -1,
86-
}),
87-
content: (heading) => [
88-
h(
89-
`span.anchor-icon`,
90-
{
91-
ariaHidden: 'true',
92-
},
93-
AnchorLinkIcon
94-
),
95-
createSROnlyLabel(toString(heading)),
96-
],
97-
},
98-
],
48+
[rehypeAutolinkHeadings, autolinkConfig],
9949
// Tweak GFM task list syntax
10050
rehypeTasklistEnhancer(),
10151
// Translates the autolink headings anchors

integrations/astro-asides.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import type { AstroIntegration } from 'astro';
2-
import { h } from 'hastscript';
32
import type * as mdast from 'mdast';
43
import remarkDirective from 'remark-directive';
54
import type * as unified from 'unified';
65
import { remove } from 'unist-util-remove';
76
import { visit } from 'unist-util-visit';
7+
import { isMDXFile } from './utils/isMDX';
8+
import { makeComponentNode } from './utils/makeComponentNode';
89

910
const AsideTagname = 'AutoImportedAside';
11+
export const asideAutoImport: Record<string, [string, string][]> = {
12+
'~/components/Aside.astro': [['default', AsideTagname]],
13+
};
1014

1115
/**
1216
* remark plugin that converts blocks delimited with `:::` into instances of
@@ -32,8 +36,8 @@ const AsideTagname = 'AutoImportedAside';
3236
function remarkAsides(): unified.Plugin<[], mdast.Root> {
3337
const variants = new Set(['note', 'tip', 'caution', 'danger']);
3438

35-
const transformer: unified.Transformer<mdast.Root> = (tree) => {
36-
visit(tree, (node) => {
39+
const transformer: unified.Transformer<mdast.Root> = (tree, file) => {
40+
visit(tree, (node, index, parent) => {
3741
if (node.type !== 'containerDirective') return;
3842
const type = node.name;
3943
if (!variants.has(type)) return;
@@ -52,9 +56,12 @@ function remarkAsides(): unified.Plugin<[], mdast.Root> {
5256
}
5357
});
5458

55-
const data = node.data || (node.data = {});
56-
data.hName = AsideTagname;
57-
data.hProperties = h(AsideTagname, { type, title }).properties;
59+
// Replace this node with the aside component it represents.
60+
parent.children[index] = makeComponentNode(
61+
AsideTagname,
62+
{ mdx: isMDXFile(file), attributes: { type, title } },
63+
...node.children
64+
);
5865
});
5966
};
6067

@@ -70,18 +77,12 @@ export function astroAsides(): AstroIntegration {
7077
return {
7178
name: '@astrojs/asides',
7279
hooks: {
73-
'astro:config:setup': ({ injectScript, updateConfig }) => {
80+
'astro:config:setup': ({ updateConfig }) => {
7481
updateConfig({
7582
markdown: {
7683
remarkPlugins: [remarkDirective, remarkAsides()],
7784
},
7885
});
79-
80-
// Auto-import the Aside component and attach it to the global scope
81-
injectScript(
82-
'page-ssr',
83-
`import ${AsideTagname} from "~/components/Aside.astro"; global.${AsideTagname} = ${AsideTagname};`
84-
);
8586
},
8687
},
8788
};

0 commit comments

Comments
 (0)