Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: import local plugins into markdown #2534

Merged
merged 9 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curly-wasps-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/markdown-remark': patch
---

Now you can use local plugins by passing a function instead of an `import`
4 changes: 3 additions & 1 deletion docs/src/pages/en/guides/markdown-content.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,16 @@ export default {
You can provide names of the plugins as well as import them:

```js
import autolinkHeadings from 'remark-autolink-headings'

// astro.config.mjs
export default {
markdownOptions: {
render: [
'@astrojs/markdown-remark',
{
remarkPlugins: [
[import('remark-autolink-headings'), { behavior: 'prepend' }],
[autolinkHeadings, { behavior: 'prepend' }],
],
},
],
Expand Down
8 changes: 2 additions & 6 deletions examples/with-markdown-plugins/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// helpful tooltips, and warnings if your exported object is invalid.
// You can disable this by removing "@ts-check" and `@type` comments below.
import astroRemark from '@astrojs/markdown-remark';
import addClasses from './add-classes.mjs';

// @ts-check
export default /** @type {import('astro').AstroUserConfig} */ ({
Expand All @@ -15,12 +16,7 @@ export default /** @type {import('astro').AstroUserConfig} */ ({
astroRemark,
{
remarkPlugins: ['remark-code-titles'],
rehypePlugins: [
['rehype-autolink-headings', { behavior: 'prepend' }],
['rehype-toc', { headings: ['h2', 'h3'] }],
[new URL('./add-classes.mjs', import.meta.url).pathname, { 'h1,h2,h3': 'title' }],
'rehype-slug',
],
rehypePlugins: [['rehype-autolink-headings', { behavior: 'prepend' }], ['rehype-toc', { headings: ['h2', 'h3'] }], [addClasses, { 'h1,h2,h3': 'title' }], 'rehype-slug'],
},
],
},
Expand Down
7 changes: 2 additions & 5 deletions packages/astro/test/astro-markdown-plugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
import markdownRemark from '@astrojs/markdown-remark';
import addClasses from '../../../examples/with-markdown-plugins/add-classes.mjs';
Copy link
Contributor

@matthewp matthewp Feb 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you possibly copy this module into the test fixture? Would be nice to not couple the tests with the examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! I also added hast-util-select as a dev dependency to ensure it's installed for the tests


describe('Astro Markdown plugins', () => {
let fixture;
Expand All @@ -15,11 +16,7 @@ describe('Astro Markdown plugins', () => {
markdownRemark,
{
remarkPlugins: ['remark-code-titles', ['rehype-autolink-headings', { behavior: 'prepend' }]],
rehypePlugins: [
[import('rehype-toc'), { headings: ['h2', 'h3'] }],
[import('../../../examples/with-markdown-plugins/add-classes.mjs'), { 'h1,h2,h3': 'title' }],
'rehype-slug',
],
rehypePlugins: [['rehype-toc', { headings: ['h2', 'h3'] }], [addClasses, { 'h1,h2,h3': 'title' }], 'rehype-slug'],
},
],
},
Expand Down
15 changes: 8 additions & 7 deletions packages/markdown/remark/src/load-plugins.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import * as unified from 'unified';
import type { Plugin, UnifiedPluginImport } from './types';
import type { Plugin } from './types';

async function importPlugin(p: string | UnifiedPluginImport): UnifiedPluginImport {
async function importPlugin(p: string | unified.Plugin): Promise<unified.Plugin> {
if (typeof p === 'string') {
return await import(p);
const importResult = await import(p);
return importResult.default;
}

return await p;
return p;
}

export function loadPlugins(items: Plugin[]): Promise<[unified.Plugin] | [unified.Plugin, any]>[] {
export function loadPlugins(items: Plugin[]): Promise<[unified.Plugin, any?]>[] {
return items.map((p) => {
return new Promise((resolve, reject) => {
if (Array.isArray(p)) {
const [plugin, opts] = p;
return importPlugin(plugin)
.then((m) => resolve([m.default, opts]))
.then((m) => resolve([m, opts]))
.catch((e) => reject(e));
}

return importPlugin(p)
.then((m) => resolve([m.default]))
.then((m) => resolve([m]))
.catch((e) => reject(e));
});
});
Expand Down
3 changes: 1 addition & 2 deletions packages/markdown/remark/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type * as unified from 'unified';
import type * as shiki from 'shiki';

export type UnifiedPluginImport = Promise<{ default: unified.Plugin }>;
export type Plugin = string | [string, any] | UnifiedPluginImport | [UnifiedPluginImport, any];
export type Plugin = string | [string, any] | unified.Plugin | [unified.Plugin, any];

export interface AstroMarkdownOptions {
mode?: 'md' | 'mdx';
Expand Down