Skip to content

Commit 9b7b6be

Browse files
authored
refactor remark custom blockquotes (#4387)
1 parent 95f6070 commit 9b7b6be

File tree

7 files changed

+135
-18
lines changed

7 files changed

+135
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@
119119
"redirect-webpack-plugin": "^1.0.0",
120120
"remark": "^13.0.0",
121121
"remark-autolink-headings": "^6.0.1",
122-
"remark-custom-blockquotes": "1.0.0",
123122
"remark-emoji": "^2.1.0",
124123
"remark-extract-anchors": "1.1.1",
125124
"remark-frontmatter": "^3.0.0",
@@ -137,6 +136,7 @@
137136
"static-site-generator-webpack-plugin": "^3.4.1",
138137
"style-loader": "^2.0.0",
139138
"tap-spot": "^1.1.1",
139+
"unist-util-visit": "^2.0.3",
140140
"webpack": "^5.11.1",
141141
"webpack-bundle-analyzer": "^4.3.0",
142142
"webpack-cli": "^4.3.1",

src/components/Markdown/Markdown.scss

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ $topHeightMobileWithBanner: $bannerHeight + $topHeightMobile;
9797

9898
p,
9999
blockquote,
100+
aside,
100101
table,
101102
pre {
102103
margin: 1em 0;
@@ -159,24 +160,21 @@ $topHeightMobileWithBanner: $bannerHeight + $topHeightMobile;
159160
}
160161
}
161162

162-
blockquote {
163+
aside {
163164
border-left: 4px solid #dddddd;
164165
padding: 0.75em 1em;
165166
color: getColor(dove-grey);
166-
font-style: italic;
167-
168167
> :first-child {
169168
margin-top: 0;
170169
}
171170
> :last-child {
172171
margin-bottom: 0;
173172
}
174-
175173
&.tip,
176174
&.warning,
177175
&.todo {
178-
border-left: none;
179-
border-radius: 3px;
176+
border-left-width: 3px;
177+
border-left-style: solid;
180178

181179
.tip-content {
182180
font-style: italic;
@@ -185,21 +183,33 @@ $topHeightMobileWithBanner: $bannerHeight + $topHeightMobile;
185183
code {
186184
color: inherit;
187185
}
186+
187+
> .tip__prefix,
188+
> .warning__prefix,
189+
> .todo__prefix {
190+
text-transform: capitalize;
191+
font-weight: 700;
192+
color: #000;
193+
font-size: getFontSize(1);
194+
}
188195
}
189196

190197
&.tip {
191198
background-color: #eaf8ff;
192199
color: #4e7182;
200+
border-left-color: darken(#eaf8ff, 40%);
193201
}
194202

195203
&.warning {
196204
background-color: #fdf5d8;
197205
color: #716b53;
206+
border-left-color: darken(#fdf5d8, 40%);
198207
}
199208

200209
&.todo {
201210
background-color: #fbddcd;
202211
color: #907a6e;
212+
border-left-color: darken(#fbddcd, 40%);
203213

204214
.tip-content::before {
205215
content: '[TODO]: ';
@@ -208,6 +218,20 @@ $topHeightMobileWithBanner: $bannerHeight + $topHeightMobile;
208218
}
209219
}
210220

221+
blockquote {
222+
border-left: 4px solid #dddddd;
223+
padding: 0.75em 1em;
224+
color: getColor(dove-grey);
225+
font-style: italic;
226+
227+
> :first-child {
228+
margin-top: 0;
229+
}
230+
> :last-child {
231+
margin-bottom: 0;
232+
}
233+
}
234+
211235
table {
212236
margin: 1em 0;
213237

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`customize blockquote should transform W> into aside of warning 1`] = `
4+
"<aside class=\\"warning\\"><h6 class=\\"warning__prefix\\">warning</h6><p>hello world</p></aside>
5+
"
6+
`;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* based on https://github.com/montogeek/remark-custom-blockquotes
3+
*/
4+
5+
const visit = require('unist-util-visit');
6+
module.exports = function customAsides(
7+
options = {
8+
mapping: {},
9+
}
10+
) {
11+
const { mapping } = options;
12+
return function transformer(tree) {
13+
// looking for `paragraph` node
14+
visit(tree, 'paragraph', visitor);
15+
function visitor(node) {
16+
const { children } = node;
17+
const textNode = children[0].value;
18+
19+
// could be link/etc.
20+
if (!textNode) return;
21+
22+
// looking for mapping in the beginning
23+
const className = mapping[textNode.substr(0, 2)];
24+
// >This is a joke <- ignore this
25+
// >T hi there
26+
const hasPostfixWhitespace = textNode.indexOf(' ') === 2;
27+
if (className && hasPostfixWhitespace) {
28+
// use `aside` element instead of `blockquote`
29+
// which I believe is more suitable as per https://developer.mozilla.org/en-US/docs/Web/HTML/Element/aside
30+
node.data = {
31+
hName: 'aside',
32+
hProperties: {
33+
className,
34+
},
35+
};
36+
37+
// remove custom characters from paragraph
38+
node.children = [
39+
{
40+
type: 'heading',
41+
depth: 6,
42+
data: {
43+
// see https://github.com/syntax-tree/mdast-util-to-hast#hname
44+
// add a className to heading
45+
hProperties: {
46+
className: `${className}__prefix`,
47+
},
48+
},
49+
children: [
50+
{
51+
type: 'text',
52+
value: `${className}`,
53+
},
54+
],
55+
},
56+
{
57+
type: 'paragraph',
58+
children: [
59+
{
60+
...children[0],
61+
value: children[0].value.slice(3),
62+
},
63+
...children.slice(1),
64+
],
65+
},
66+
];
67+
}
68+
}
69+
};
70+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import remark from 'remark';
2+
describe('customize blockquote', () => {
3+
it('should transform W> into aside of warning', () => {
4+
remark()
5+
.use(require('./index.js'), {
6+
mapping: {
7+
'W>': 'warning',
8+
},
9+
})
10+
.use(require('remark-html'))
11+
.process(
12+
`
13+
W> hello world
14+
`,
15+
function (err, { contents }) {
16+
expect(err).toBeNull();
17+
expect(contents).toContain('<aside class="warning"');
18+
expect(contents).toContain('<h6 class="warning__prefix"');
19+
expect(contents).toContain('warning');
20+
expect(contents).toMatchSnapshot();
21+
}
22+
);
23+
});
24+
});

webpack.common.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ const mdPlugins = [
99
remarkResponsiveTable,
1010
require('remark-emoji'),
1111
[
12-
require('remark-custom-blockquotes'),
12+
require('./src/remark-plugins/remark-custom-asides/index.js'),
1313
{
1414
mapping: {
1515
'T>': 'tip',
1616
'W>': 'warning',
17-
'?>': 'todo'
18-
}
19-
}
17+
'?>': 'todo',
18+
},
19+
},
2020
],
2121
[
2222
require('remark-autolink-headings'),

yarn.lock

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11218,13 +11218,6 @@ remark-autolink-headings@^6.0.1:
1121811218
extend "^3.0.0"
1121911219
unist-util-visit "^2.0.0"
1122011220

11221-
remark-custom-blockquotes@1.0.0:
11222-
version "1.0.0"
11223-
resolved "https://registry.yarnpkg.com/remark-custom-blockquotes/-/remark-custom-blockquotes-1.0.0.tgz#82ed4cebf45255a0d07ba81696f909c23b1d749d"
11224-
integrity sha512-R6/tiD4RE7rIaPAmZcVdbddH35DJraxNiVWo8nwF4QIhjwthClAkrzDzKcRHzf2sHL3OlG5jOBtwvZX7Dwww/w==
11225-
dependencies:
11226-
unist-util-visit "1.3.1"
11227-
1122811221
remark-emoji@^2.1.0:
1122911222
version "2.1.0"
1123011223
resolved "https://registry.yarnpkg.com/remark-emoji/-/remark-emoji-2.1.0.tgz#69165d1181b98a54ad5d9ef811003d53d7ebc7db"

0 commit comments

Comments
 (0)