Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit 28a66ff

Browse files
authored
Fix RDMD backwards compatible anchors. (#669)
1 parent 6f280a5 commit 28a66ff

File tree

7 files changed

+23
-62
lines changed

7 files changed

+23
-62
lines changed

packages/markdown/__tests__/__snapshots__/index.test.js.snap

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,13 @@ exports[`emojis 1`] = `
3737
3838
exports[`export multiple Markdown renderers allows complex compact headings 1`] = `
3939
"<h1 id=\\"basic-text\\">Basic Text</h1>
40-
<div id=\\"section-basic-text\\"></div>
4140
<h2 id=\\"-oh-noes\\">🙀 oh noes!</h2>
42-
<div id=\\"section--oh-noes\\"></div>
4341
<h3 id=\\"6-oh-no\\"><strong>6</strong>. Oh No</h3>
44-
<div id=\\"section-6-oh-no\\"></div>
4542
<p>Lorem ipsum dolor!</p>"
4643
`;
4744
4845
exports[`export multiple Markdown renderers renders HTML 1`] = `
4946
"<h1 id=\\"hello-world\\">Hello World</h1>
50-
<div id=\\"section-hello-world\\"></div>
5147
5248
5349
@@ -96,11 +92,6 @@ exports[`export multiple Markdown renderers renders custom React components 1`]
9692
</Unknown>
9793
9894
99-
<div
100-
id="section-hello-world"
101-
/>
102-
103-
10495
10596
10697
@@ -297,19 +288,6 @@ Object {
297288
Object {
298289
"type": "text",
299290
"value": "
300-
",
301-
},
302-
Object {
303-
"children": Array [],
304-
"properties": Object {
305-
"id": "section-hello-world",
306-
},
307-
"tagName": "div",
308-
"type": "element",
309-
},
310-
Object {
311-
"type": "text",
312-
"value": "
313291
314292
315293
@@ -837,11 +815,6 @@ exports[`export multiple Markdown renderers renders plain markdown as React 1`]
837815
</h1>
838816
839817
840-
<div
841-
id="section-hello-world"
842-
/>
843-
844-
845818
846819
847820
@@ -1029,9 +1002,6 @@ exports[`list items 1`] = `
10291002
10301003
exports[`magic image 1`] = `"<figure><img src=\\"https://files.readme.io/6f52e22-man-eating-pizza-and-making-an-ok-gesture.jpg\\" title=\\"man-eating-pizza-and-making-an-ok-gesture.jpg\\" class=\\"\\" width=\\"100%\\" align=\\"\\" alt=\\"\\" caption=\\"\\" height=\\"auto\\" loading=\\"lazy\\"><span class=\\"lightbox\\" role=\\"dialog\\"><span class=\\"lightbox-inner\\"><img src=\\"https://files.readme.io/6f52e22-man-eating-pizza-and-making-an-ok-gesture.jpg\\" title=\\"Click to close...\\" class=\\"\\" width=\\"100%\\" align=\\"\\" alt=\\"\\" caption=\\"\\" height=\\"auto\\" loading=\\"lazy\\"></span></span><figcaption><p>A guy. Eating pizza. And making an OK gesture.</p></figcaption></figure>"`;
10311004
1032-
exports[`prefix anchors with section- should add a section- prefix to heading anchors 1`] = `
1033-
"<h1 id=\\"heading\\">heading</h1>
1034-
<div id=\\"section-heading\\"></div>"
1035-
`;
1005+
exports[`prefix anchors with section- should add a section- prefix to heading anchors 1`] = `"<h1 id=\\"heading\\">heading</h1>"`;
10361006
10371007
exports[`tables 1`] = `"<div class=\\"rdmd-table\\"><div class=\\"rdmd-table-inner\\"><table><thead><tr><th>Tables</th><th style=\\"text-align: center;\\">Are</th><th style=\\"text-align: right;\\">Cool</th></tr></thead><tbody><tr><td>col 3 is</td><td style=\\"text-align: center;\\">right-aligned</td><td style=\\"text-align: right;\\">$1600</td></tr><tr><td>col 2 is</td><td style=\\"text-align: center;\\">centered</td><td style=\\"text-align: right;\\">$12</td></tr><tr><td>zebra stripes</td><td style=\\"text-align: center;\\">are neat</td><td style=\\"text-align: right;\\">$1</td></tr></tbody></table></div></div>"`;

packages/markdown/components/Heading/index.jsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
const React = require('react');
22
const PropTypes = require('prop-types');
3+
const kebab = require('lodash/kebabCase');
4+
5+
/* istanbul ignore next */
6+
function deprecatedGenerateHeadingId(e) {
7+
if (typeof e === 'object') {
8+
return deprecatedGenerateHeadingId(e.props.children[0]);
9+
}
10+
return kebab(e);
11+
}
312

413
function Heading({ tag, ...props }) {
514
if (!props.children) return '';
@@ -9,6 +18,11 @@ function Heading({ tag, ...props }) {
918
align: props.align,
1019
};
1120
return React.createElement(tag, attrs, [
21+
<div
22+
key={`heading-anchor-${props.id}-deprecated1`}
23+
className="heading-anchor-deprecated"
24+
id={`section-${deprecatedGenerateHeadingId(props.id)}`}
25+
/>,
1226
<div key={`heading-anchor-${props.id}`} className="heading-anchor anchor waypoint" id={props.id} />,
1327
<div key={`heading-text-${props.id}`} className="heading-text">
1428
{props.children}

packages/markdown/components/Heading/style.scss

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
.heading-text {
77
flex: 1 100%;
88
}
9+
.heading-anchor-deprecated {
10+
position: absolute;
11+
top: 0;
12+
}
913
.heading-anchor {
1014
top: -1rem !important;
1115
}
12-
.heading-anchor, .heading-anchor-icon {
16+
.heading-anchor,
17+
.heading-anchor-icon {
1318
position: absolute !important;
1419
display: inline !important;
1520
order: -1;

packages/markdown/index.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ const {
6262
rdmePinCompiler,
6363
} = require('./processor/compile');
6464

65-
/* Custom Unified Plugins
66-
*/
67-
const sectionAnchorId = require('./processor/plugin/section-anchor-id');
68-
6965
// Processor Option Defaults
7066
const options = require('./options.json');
7167

@@ -148,7 +144,6 @@ export function processor(opts = {}) {
148144
.use(!opts.correctnewlines ? remarkBreaks : () => {})
149145
.use(gemojiParser.sanitize(sanitize))
150146
.use(remarkSlug)
151-
.use(sectionAnchorId)
152147
.use(remarkRehype, { allowDangerousHtml: true })
153148
.use(rehypeRaw)
154149
.use(rehypeSanitize, sanitize);

packages/markdown/package-lock.json

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/markdown/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@readme/variable": "^6.4.0",
1515
"copy-to-clipboard": "^3.3.1",
1616
"hast-util-sanitize": "^1.2.0",
17+
"lodash": "^4.17.15",
1718
"prop-types": "^15.7.2",
1819
"react": "^16.4.2",
1920
"rehype-raw": "^4.0.1",
@@ -27,7 +28,6 @@
2728
"remark-slug": "^6.0.0",
2829
"remark-stringify": "^8.0.0",
2930
"unified": "^8.4.0",
30-
"unist-util-flatmap": "^1.0.0",
3131
"unist-util-map": "^2.0.0",
3232
"unist-util-visit": "^2.0.1"
3333
},

packages/markdown/processor/plugin/section-anchor-id.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)