Skip to content

Commit f833319

Browse files
committed
fix: create a remark plugin that fixes fenced code block parsing issues
1 parent 75dd98b commit f833319

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

gatsby-config.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const fs = require('fs');
22
const path = require('path');
3-
const indentedCodeBlock = require('./codemods/indentedCodeBlock');
43

54
const siteUrl = 'https://docs.newrelic.com';
65

@@ -181,7 +180,7 @@ module.exports = {
181180
// https://github.com/mdx-js/mdx/issues/1283
182181
//
183182
// If this is addressed in MDX v2, we can safely remove this.
184-
remarkPlugins: [indentedCodeBlock],
183+
remarkPlugins: [],
185184
gatsbyRemarkPlugins: [
186185
{
187186
resolve: 'gatsby-remark-images',
@@ -217,6 +216,11 @@ module.exports = {
217216
maxWidth: 1200,
218217
},
219218
},
219+
{
220+
resolve: require.resolve(
221+
'./plugins/gatsby-remark-mdx-v2-fenced-code-blocks'
222+
),
223+
},
220224
],
221225
},
222226
},
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { createCompiler } = require('@mdx-js/mdx');
2+
const visit = require('unist-util-visit');
3+
4+
const INVALID_CODE_BLOCK = /`{3,}\n/;
5+
6+
const compiler = createCompiler();
7+
8+
module.exports = ({ markdownAST }) => {
9+
visit(
10+
markdownAST,
11+
(node) => node.type === 'code' && INVALID_CODE_BLOCK.test(node.value),
12+
(node, idx, parent) => {
13+
const idxOfCode = node.value.indexOf('```');
14+
const text = node.value.slice(0, idxOfCode);
15+
const parsed = compiler.parse(
16+
node.value.slice(idxOfCode).replace(/^`+/, '')
17+
);
18+
19+
node.value = text.trim();
20+
parent.children.splice(idx + 1, 0, ...parsed.children);
21+
}
22+
);
23+
24+
return markdownAST;
25+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)