Skip to content

Commit

Permalink
refactor: githubFlavoredMarkdown -> gfm
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Jan 3, 2023
1 parent 91edc65 commit 4a2c2e7
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 38 deletions.
14 changes: 7 additions & 7 deletions .changeset/shaggy-keys-turn.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Refine Markdown and MDX configuration options for ease-of-use.
#### Markdown

- **Remove `remark-smartypants`** from Astro's default Markdown plugins.
- **Replace the `extendDefaultPlugins` option** with a simplified `githubFlavoredMarkdown` boolean. This is enabled by default, and can be disabled to remove GFM.
- Ensure GitHub-Flavored Markdown is applied whether or not custom `remarkPlugins` or `rehypePlugins` are configured. If you want to apply custom plugins _and_ remove GFM, manually set `githubFlavoredMarkdown: false` in your config.
- **Replace the `extendDefaultPlugins` option** with a simplified `gfm` boolean. This is enabled by default, and can be disabled to remove GitHub-Flavored Markdown.
- Ensure GitHub-Flavored Markdown is applied whether or not custom `remarkPlugins` or `rehypePlugins` are configured. If you want to apply custom plugins _and_ remove GFM, manually set `gfm: false` in your config.

#### MDX

Expand All @@ -37,9 +37,9 @@ export default defineConfig({
});
```

##### Migrate `extendDefaultPlugins` to `githubFlavoredMarkdown`
##### Migrate `extendDefaultPlugins` to `gfm`

You may have disabled Astro's built-in plugins (GitHub-Flavored Markdown and Smartypants) with the `extendDefaultPlugins` option. Since Smartypants has been removed, this has been renamed to `githubFlavoredMarkdown`.
You may have disabled Astro's built-in plugins (GitHub-Flavored Markdown and Smartypants) with the `extendDefaultPlugins` option. Since Smartypants has been removed, this has been renamed to `gfm`.

```diff
// astro.config.mjs
Expand All @@ -48,16 +48,16 @@ import { defineConfig } from 'astro/config';
export default defineConfig({
markdown: {
- extendDefaultPlugins: false,
+ githubFlavoredMarkdown: false,
+ gfm: false,
}
});
```


Additionally, applying remark and rehype plugins **no longer disables** `githubFlavoredMarkdown`. You will need to opt-out manually by setting `githubFlavoredMarkdown` to `false`.
Additionally, applying remark and rehype plugins **no longer disables** `gfm`. You will need to opt-out manually by setting `gfm` to `false`.

##### Migrate MDX's `extendPlugins` to `extendMarkdownConfig`

You may have used the `extendPlugins` option to manage plugin defaults in MDX. This has been replaced by 2 flags:
- `extendMarkdownConfig` (`true` by default) to toggle Markdown config inheritance. This replaces the `extendPlugins: 'markdown'` option.
- `githubFlavoredMarkdown` (`true` by default) to toggle GitHub-Flavored Markdown in MDX. This replaces the `extendPlugins: 'defaults'` option.
- `gfm` (`true` by default) to toggle GitHub-Flavored Markdown in MDX. This replaces the `extendPlugins: 'defaults'` option.
8 changes: 4 additions & 4 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,21 +763,21 @@ export interface AstroUserConfig {
rehypePlugins?: RehypePlugins;
/**
* @docs
* @name markdown.githubFlavoredMarkdown
* @name markdown.gfm
* @type {boolean}
* @default `true`
* @description
* Astro uses [GitHub-flavored Markdown](https://github.com/remarkjs/remark-gfm) by default. To disable this, set the `githubFlavoredMarkdown` flag to `false`:
* Astro uses [GitHub-flavored Markdown](https://github.com/remarkjs/remark-gfm) by default. To disable this, set the `gfm` flag to `false`:
*
* ```js
* {
* markdown: {
* githubFlavoredMarkdown: false,
* gfm: false,
* }
* }
* ```
*/
githubFlavoredMarkdown?: boolean;
gfm?: boolean;
/**
* @docs
* @name markdown.remarkRehype
Expand Down
4 changes: 1 addition & 3 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ export const AstroConfigSchema = z.object({
.custom<RemarkRehype>((data) => data instanceof Object && !Array.isArray(data))
.optional()
.default(ASTRO_CONFIG_DEFAULTS.markdown.remarkRehype),
githubFlavoredMarkdown: z
.boolean()
.default(ASTRO_CONFIG_DEFAULTS.markdown.githubFlavoredMarkdown),
gfm: z.boolean().default(ASTRO_CONFIG_DEFAULTS.markdown.gfm),
})
.default({}),
vite: z
Expand Down
8 changes: 4 additions & 4 deletions packages/astro/test/astro-markdown-plugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,20 @@ describe('Astro Markdown plugins', () => {
expect($('#github-flavored-markdown-test').hasClass('title')).to.equal(true);
});

for (const githubFlavoredMarkdown of [true, false]) {
it(`Handles GFM when githubFlavoredMarkdown = ${githubFlavoredMarkdown}`, async () => {
for (const gfm of [true, false]) {
it(`Handles GFM when gfm = ${gfm}`, async () => {
const fixture = await buildFixture({
markdown: {
remarkPlugins: [remarkExamplePlugin],
rehypePlugins: [[addClasses, { 'h1,h2,h3': 'title' }]],
githubFlavoredMarkdown,
gfm,
},
});
const html = await fixture.readFile('/with-gfm/index.html');
const $ = cheerio.load(html);

// test 1: GFM autolink applied correctly
if (githubFlavoredMarkdown === true) {
if (gfm === true) {
expect($('a[href="https://example.com"]')).to.have.lengthOf(1);
} else {
expect($('a[href="https://example.com"]')).to.have.lengthOf(0);
Expand Down
8 changes: 4 additions & 4 deletions packages/integrations/mdx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default defineConfig({
remarkPlugins: [remarkToc],
rehypePlugins: [rehypeMinifyHtml],
remarkRehype: { footnoteLabel: 'Footnotes' },
githubFlavoredMarkdown: false,
gfm: false,
})
]
})
Expand Down Expand Up @@ -137,7 +137,7 @@ export default defineConfig({
markdown: {
syntaxHighlight: 'prism',
remarkPlugins: [remarkPlugin1],
githubFlavoredMarkdown: true,
gfm: true,
},
integrations: [
mdx({
Expand All @@ -146,8 +146,8 @@ export default defineConfig({
// Markdown `remarkPlugins` ignored,
// only `remarkPlugin2` applied.
remarkPlugins: [remarkPlugin2],
// `githubFlavoredMarkdown` overridden to `false`
githubFlavoredMarkdown: false,
// `gfm` overridden to `false`
gfm: false,
})
]
});
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/mdx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function applyDefaultOptions({
extendMarkdownConfig: options.extendMarkdownConfig ?? defaults.extendMarkdownConfig,
recmaPlugins: options.recmaPlugins ?? defaults.recmaPlugins,
remarkRehype: options.remarkRehype ?? defaults.remarkRehype,
githubFlavoredMarkdown: options.githubFlavoredMarkdown ?? defaults.githubFlavoredMarkdown,
gfm: options.gfm ?? defaults.gfm,
remarkPlugins: options.remarkPlugins ?? defaults.remarkPlugins,
rehypePlugins: options.rehypePlugins ?? defaults.rehypePlugins,
shikiConfig: options.shikiConfig ?? defaults.shikiConfig,
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/mdx/src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export async function getRemarkPlugins(
if (mdxOptions.syntaxHighlight === 'prism') {
remarkPlugins.push(remarkPrism);
}
if (mdxOptions.githubFlavoredMarkdown) {
if (mdxOptions.gfm) {
remarkPlugins.push(remarkGfm);
}

Expand Down
14 changes: 4 additions & 10 deletions packages/integrations/mdx/test/mdx-plugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('MDX plugins', () => {
fixture = await buildFixture({
markdown: {
remarkPlugins: [remarkToc],
githubFlavoredMarkdown: false,
gfm: false,
},
integrations: [
mdx({
Expand Down Expand Up @@ -119,20 +119,14 @@ describe('MDX plugins', () => {
).to.be.null;
});

it('Handles githubFlavoredMarkdown', async () => {
it('Handles gfm', async () => {
const html = await fixture.readFile(FILE);
const { document } = parseHTML(html);

if (extendMarkdownConfig === true) {
expect(
selectGfmLink(document),
'Does not respect `markdown.githubFlavoredMarkdown` option.'
).to.be.null;
expect(selectGfmLink(document), 'Does not respect `markdown.gfm` option.').to.be.null;
} else {
expect(
selectGfmLink(document),
'Respects `markdown.githubFlavoredMarkdown` unexpectedly.'
).to.not.be.null;
expect(selectGfmLink(document), 'Respects `markdown.gfm` unexpectedly.').to.not.be.null;
}
});
});
Expand Down
6 changes: 3 additions & 3 deletions packages/markdown/remark/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const markdownConfigDefaults: Omit<Required<AstroMarkdownOptions>, 'draft
remarkPlugins: [],
rehypePlugins: [],
remarkRehype: {},
githubFlavoredMarkdown: true,
gfm: true,
};

/** Shared utility for rendering markdown */
Expand All @@ -57,7 +57,7 @@ export async function renderMarkdown(
remarkPlugins = markdownConfigDefaults.remarkPlugins,
rehypePlugins = markdownConfigDefaults.rehypePlugins,
remarkRehype = markdownConfigDefaults.remarkRehype,
githubFlavoredMarkdown = markdownConfigDefaults.githubFlavoredMarkdown,
gfm = markdownConfigDefaults.gfm,
isAstroFlavoredMd = false,
isExperimentalContentCollections = false,
contentDir,
Expand All @@ -71,7 +71,7 @@ export async function renderMarkdown(
.use(toRemarkInitializeAstroData({ userFrontmatter }))
.use(isAstroFlavoredMd ? [remarkMdxish, remarkMarkAndUnravel, remarkUnwrap, remarkEscape] : []);

if (githubFlavoredMarkdown) {
if (gfm) {
parser.use(remarkGfm);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/markdown/remark/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface AstroMarkdownOptions {
remarkPlugins?: RemarkPlugins;
rehypePlugins?: RehypePlugins;
remarkRehype?: RemarkRehype;
githubFlavoredMarkdown?: boolean;
gfm?: boolean;
}

export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
Expand Down

0 comments on commit 4a2c2e7

Please sign in to comment.