Skip to content

Commit

Permalink
make Remark rehype options available in astro config (#4138)
Browse files Browse the repository at this point in the history
* make remark-rehype config available in astro.config.mjs

* add test for remark-rehype config, checks that footnotes can be translated

* update lockfile to take the added test into account

* omit handlers and unkownHandler from the RemarkRehype type

* define RemarkRehype with proper references to the handler and handlers types

* formatting

* changeset

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
Co-authored-by: Matthew Phillips <matthew@skypack.dev>
  • Loading branch information
3 people authored Aug 25, 2022
1 parent b680c3e commit 839097c
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/giant-pandas-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': minor
'@astrojs/markdown-remark': minor
---

Makes remark-rehype options available in astro.config.mjs
18 changes: 18 additions & 0 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
MarkdownRenderingResult,
RehypePlugins,
RemarkPlugins,
RemarkRehype,
ShikiConfig,
} from '@astrojs/markdown-remark';
import type * as babel from '@babel/core';
Expand Down Expand Up @@ -680,6 +681,23 @@ export interface AstroUserConfig {
* ```
*/
rehypePlugins?: RehypePlugins;
/**
* @docs
* @name markdown.remarkRehype
* @type {RemarkRehype}
* @description
* Pass options to [remark-rehype](https://github.com/remarkjs/remark-rehype#api) .
*
* ```js
* {
* markdown: {
* // Example: Translate the footnotes text to another language, here are the default English values
* remarkRehype: { footnoteLabel: "Footnotes", footnoteBackLabel: "Back to content"},
* },
* };
* ```
*/
remarkRehype?: RemarkRehype;
};

/**
Expand Down
7 changes: 6 additions & 1 deletion packages/astro/src/core/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RehypePlugin, RemarkPlugin } from '@astrojs/markdown-remark';
import type { RehypePlugin, RemarkPlugin, RemarkRehype } from '@astrojs/markdown-remark';
import type * as Postcss from 'postcss';
import type { ILanguageRegistration, IThemeRegistration, Theme } from 'shiki';
import type { Arguments as Flags } from 'yargs-parser';
Expand Down Expand Up @@ -51,6 +51,7 @@ const ASTRO_CONFIG_DEFAULTS: AstroUserConfig & any = {
},
remarkPlugins: [],
rehypePlugins: [],
remarkRehype: {},
},
vite: {},
legacy: {
Expand Down Expand Up @@ -214,6 +215,10 @@ export const AstroConfigSchema = z.object({
])
.array()
.default(ASTRO_CONFIG_DEFAULTS.markdown.rehypePlugins),
remarkRehype: z
.custom<RemarkRehype>((data) => data instanceof Object && !Array.isArray(data))
.optional()
.default(ASTRO_CONFIG_DEFAULTS.markdown.remarkRehype),
})
.default({}),
vite: z
Expand Down
43 changes: 43 additions & 0 deletions packages/astro/test/astro-markdown-remarkRehype.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Astro Markdown without remark-rehype config', () => {
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-markdown-remarkRehype/',
});
await fixture.build();
});
it('Renders footnotes with default English labels', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
expect($('#footnote-label').text()).to.equal('Footnotes');
expect($('.data-footnote-backref').first().attr('aria-label')).to.equal('Back to content');
});
});

describe('Astro Markdown with remark-rehype config', () => {
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-markdown-remarkRehype/',
markdown: {
remarkRehype: {
footnoteLabel: 'Catatan kaki',
footnoteBackLabel: 'Kembali ke konten',
},
},
});
await fixture.build();
});
it('Renders footnotes with values from the configuration', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
expect($('#footnote-label').text()).to.equal('Catatan kaki');
expect($('.data-footnote-backref').first().attr('aria-label')).to.equal('Kembali ke konten');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/astro-markdown-remarkRehype",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
foo: bar
---

# Hello world

This[^1] should be visible.

[^1]: And there would be a footnote.
2 changes: 2 additions & 0 deletions packages/markdown/remark/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export async function renderMarkdown(
shikiConfig = {},
remarkPlugins = [],
rehypePlugins = [],
remarkRehype = {},
isAstroFlavoredMd = false,
} = opts;
const input = new VFile({ value: content, path: fileURL });
Expand Down Expand Up @@ -85,6 +86,7 @@ export async function renderMarkdown(
'mdxTextExpression',
]
: [],
...remarkRehype,
},
],
]);
Expand Down
10 changes: 10 additions & 0 deletions packages/markdown/remark/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type * as hast from 'hast';
import type * as mdast from 'mdast';
import type { ILanguageRegistration, IThemeRegistration, Theme } from 'shiki';
import type {
Options as RemarkRehypeOptions,
all as Handlers,
one as Handler,
} from 'remark-rehype';
import type * as unified from 'unified';
import type { VFile } from 'vfile';

Expand All @@ -20,6 +25,10 @@ export type RehypePlugin<PluginParameters extends any[] = any[]> = unified.Plugi

export type RehypePlugins = (string | [string, any] | RehypePlugin | [RehypePlugin, any])[];

export type RemarkRehype = Omit<RemarkRehypeOptions, 'handlers' | 'unknownHandler'> & {
handlers: typeof Handlers;
} & { handler: typeof Handler };

export interface ShikiConfig {
langs?: ILanguageRegistration[];
theme?: Theme | IThemeRegistration;
Expand All @@ -33,6 +42,7 @@ export interface AstroMarkdownOptions {
shikiConfig?: ShikiConfig;
remarkPlugins?: RemarkPlugins;
rehypePlugins?: RehypePlugins;
remarkRehype?: RemarkRehype;
}

export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 839097c

Please sign in to comment.