Skip to content

Commit

Permalink
fix: import local plugins into markdown (withastro#2534)
Browse files Browse the repository at this point in the history
* Replaced "UnifiedPluginImport" for a function

* Updated tests

* Updated docs

* Updated examples

* Added changeset

* Fixed tests

* Removed unused "UnifiedPluginImport"

* Duplicated add-classes.mjs
  • Loading branch information
JuanM04 authored Feb 4, 2022
1 parent 875bffb commit 3dbf654
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
"@types/yargs-parser": "^20.2.1",
"chai": "^4.3.4",
"cheerio": "^1.0.0-rc.10",
"hast-util-select": "^5.0.1",
"mocha": "^9.1.3",
"vite": "~2.6.10"
},
Expand Down
7 changes: 2 additions & 5 deletions 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 './fixtures/astro-markdown-plugins/add-classes.mjs';

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
18 changes: 18 additions & 0 deletions test/fixtures/astro-markdown-plugins/add-classes.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { selectAll } from 'hast-util-select';

export default (additions) => {
const adders = Object.entries(additions).map(adder);
return (node) => adders.forEach((a) => a(node));
};

const adder = ([selector, className]) => {
const writer = write(className);
return (node) => selectAll(selector, node).forEach(writer);
};

const write =
(className) =>
({ properties }) => {
if (!properties.className) properties.className = className;
else properties.className += ` ${className}`;
};

0 comments on commit 3dbf654

Please sign in to comment.