-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy path.remarkrc.mjs
More file actions
128 lines (124 loc) · 4.2 KB
/
.remarkrc.mjs
File metadata and controls
128 lines (124 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import remarkFrontmatter from 'remark-frontmatter';
import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';
import remarkMdx from 'remark-mdx';
import unifiedConsistency from 'unified-consistency';
import stringWidth from 'string-width';
import { visitParents, SKIP } from 'unist-util-visit-parents';
import { generate } from 'astring';
/**
* @import {} from 'remark-stringify'
* @type import('unified').Preset
*/
const remarkConfig = {
settings: {
bullet: '-',
emphasis: '_',
rule: '-',
incrementListMarker: false,
tightDefinitions: true,
},
plugins: [
remarkFrontmatter,
remarkMath,
[
remarkGfm,
{
singleTilde: false,
stringLength: stringWidth,
},
],
[
remarkMdx,
{
printWidth: 20,
},
],
function formatJsxElements() {
return (tree, file) => {
// a JSX element embedded in flow (block)
visitParents(tree, 'mdxJsxFlowElement', (node, ancestors) => {
try {
if (!node.attributes) { return; }
for (const attr of node.attributes) {
if (
attr.type === 'mdxJsxAttribute' &&
attr.value?.type === 'mdxJsxAttributeValueExpression' &&
attr.value.data?.estree
) {
const expr = attr.value;
// Slighly trim single-line expressions
if (typeof expr.value === 'string' && !expr.value.trim().includes('\n')) {
expr.value = expr.value.trim();
delete expr.data.estree;
continue;
}
// Multi-line expressions
if (!expr.data) { continue; }
const indent = ancestors.length === 0 ? 0 : ancestors.length;
const formatted = generate(expr.data.estree.body[0].expression, {
startingIndentLevel: indent,
});
expr.value = formatted;
delete expr.data.estree;
}
}
} catch (_) {
// NOTE: Let's silently do nothing — this is the default behavior anyways
}
});
// a JSX element embedded in text (span, inline)
visitParents(tree, 'mdxJsxTextElement', (node) => {
try {
if (!node.attributes) { return SKIP; }
for (const attr of node.attributes) {
if (
attr.type === 'mdxJsxAttribute' &&
attr.value?.type === 'mdxJsxAttributeValueExpression' &&
attr.value.data?.estree
) {
const expr = attr.value;
if (!expr.data) { continue; }
const formatted = generate(expr.data.estree.body[0].expression);
expr.value = formatted;
delete expr.data.estree;
}
}
return SKIP;
} catch (_) {
// NOTE: Let's silently do nothing — this is the default behavior anyways
}
});
// a JavaScript expression embedded in flow (block)
visitParents(tree, 'mdxFlowExpression', (node) => {
try {
if (!node.data) { return SKIP; }
const formatted = generate(node.data.estree.body[0].expression);
node.value = formatted;
delete node.data.estree;
return SKIP;
} catch (_) {
// NOTE: Let's silently do nothing — this is the default behavior anyways
}
});
// a JavaScript expression embedded in text (span, inline)
visitParents(tree, 'mdxTextExpression', (node) => {
try {
if (!node.data) { return SKIP; }
const formatted = generate(node.data.estree.body[0].expression);
node.value = formatted;
delete node.data.estree;
return SKIP;
} catch (_) {
// NOTE: Let's silently do nothing — this is the default behavior anyways
// console.error(
// `Could not format a node in the file ${file.path}: ${JSON.stringify(node)}`
// );
}
});
};
},
unifiedConsistency,
],
};
export default remarkConfig;