Skip to content

Commit 6e44f7e

Browse files
fix: readme protocol links (#1189)
| [![PR App][icn]][demo] | Fix RM-XYZ | | :--------------------: | :--------: | ## 🧰 Changes Fixes sanitization of readme style protocol links `[label](doc:slug)`. We recently switched our plain markdown parsing to use the latest version of this package with `{ format: 'md' }`. We want to eventually remove `@readme/markdown@6`, but it also helped reduce our bundle size. In the process, we failed to port over our custom sanitization schema. For the most part, I think this is fine to drop support for old, custom markdown features. But it does break our custom protocol links, which at least one enterprise customer was using. ## 🧬 QA & Testing - [Broken on production][prod]. - [Working in this PR app][demo]. [demo]: https://markdown-pr-PR_NUMBER.herokuapp.com [prod]: https://SUBDOMAIN.readme.io [icn]: https://user-images.githubusercontent.com/886627/160426047-1bee9488-305a-4145-bb2b-09d8b757d38a.svg
1 parent d517744 commit 6e44f7e

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

__tests__/lib/compile.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { compile } from '../../index';
2+
3+
describe('compile', () => {
4+
describe("{ format: 'md' }", () => {
5+
it('returns plain text of markdown components', () => {
6+
const md = '[link to doc](doc:getting-started)';
7+
8+
const tree = compile(md, { format: 'md' });
9+
expect(tree).toMatch(/href: "doc:getting-started"/);
10+
});
11+
});
12+
});

lib/compile.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import type { CompileOptions } from '@mdx-js/mdx';
22
import type { PluggableList } from 'unified';
33

44
import { compileSync as mdxCompileSync } from '@mdx-js/mdx';
5+
import deepmerge from 'deepmerge';
56
import rehypeRaw from 'rehype-raw';
6-
import rehypeSanitize from 'rehype-sanitize';
7+
import rehypeSanitize, { defaultSchema } from 'rehype-sanitize';
78
import remarkFrontmatter from 'remark-frontmatter';
89
import remarkGfm from 'remark-gfm';
910

@@ -22,6 +23,10 @@ export type CompileOpts = CompileOptions & {
2223

2324
const { codeTabsTransformer, ...transforms } = defaultTransforms;
2425

26+
const sanitizeSchema = deepmerge(defaultSchema, {
27+
protocols: ['doc', 'ref', 'blog', 'changelog', 'page'],
28+
});
29+
2530
const compile = (
2631
text: string,
2732
{ components = {}, missingComponents, copyButtons, useTailwind, ...opts }: CompileOpts = {},
@@ -58,7 +63,7 @@ const compile = (
5863
passThrough: ['mdxjsEsm'],
5964
},
6065
]);
61-
rehypePlugins.push(rehypeSanitize);
66+
rehypePlugins.push([rehypeSanitize, sanitizeSchema]);
6267
}
6368

6469
try {

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@readme/variable": "^18.0.0",
3131
"copy-to-clipboard": "^3.3.2",
3232
"debug": "^4.3.4",
33+
"deepmerge": "^4.3.1",
3334
"emoji-regex": "^10.2.1",
3435
"estree-util-value-to-estree": "^3.1.1",
3536
"gemoji": "^8.1.0",

0 commit comments

Comments
 (0)