Skip to content

Commit 69d71fc

Browse files
yang1666204liyang
andauthored
[fix]:Fixed the problem of invalid bolding in markdown documents (#1679)
Co-authored-by: liyang <[email protected]>
1 parent f3c9ccc commit 69d71fc

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

config/markdown-bold-plugin.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const { visit } = require('unist-util-visit');
2+
3+
function getFirstBoldContent(str, startIdx = 0) {
4+
const strArr = str.split("");
5+
for (let i = startIdx; i < strArr.length; i++) {
6+
if (
7+
strArr[i - 1] !== "*" &&
8+
strArr[i] === "*" &&
9+
strArr[i + 1] === "*" &&
10+
strArr[i + 2] !== "*"
11+
) {
12+
// start with **
13+
let j;
14+
for (j = i + 2; j < strArr.length; j++) {
15+
if (
16+
strArr[j - 1] !== "*" &&
17+
strArr[j] === "*" &&
18+
strArr[j + 1] === "*" &&
19+
strArr[j + 2] !== "*"
20+
) {
21+
// end with **
22+
return {
23+
start: i,
24+
end: j,
25+
};
26+
}
27+
}
28+
}
29+
}
30+
return null;
31+
}
32+
33+
const plugin = options => {
34+
const transformer = async (ast, file) => {
35+
visit(ast, 'text', (node, index, parent) => {
36+
if (getFirstBoldContent(node.value)) {
37+
const { start, end } = getFirstBoldContent(node.value);
38+
const value = node.value.slice(start + 2, end);
39+
parent.children[index] = {
40+
type: 'strong',
41+
children: [
42+
{
43+
value: value,
44+
type: 'text',
45+
},
46+
],
47+
};
48+
const [boldBefore, , boldAfter] = node.value.split('**');
49+
let hasBoldBefore = !!boldBefore;
50+
if (boldBefore) {
51+
parent.children.splice(index, 0, {
52+
type: 'text',
53+
value: boldBefore,
54+
});
55+
}
56+
if (boldAfter) {
57+
parent.children.splice(hasBoldBefore ? index + 2 : index + 1, 0, {
58+
type: 'text',
59+
value: boldAfter,
60+
});
61+
}
62+
console.warn(
63+
`The bold syntax of "${value}" in "${file.path}" is invalid and is being automatically optimized`,
64+
);
65+
}
66+
});
67+
};
68+
return transformer;
69+
};
70+
71+
module.exports = {
72+
markdownBoldPlugin: plugin,
73+
};

docusaurus.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { ssrTemplate } = require('./config/ssrTemplate');
33
const customDocusaurusPlugin = require('./config/custom-docusaurus-plugin');
44
const versionsPlugin = require('./config/versions-plugin');
55
const VERSIONS = require('./versions.json');
6+
const { markdownBoldPlugin } = require('./config/markdown-bold-plugin');
67
const lightCodeTheme = themes.dracula;
78

89
const logoImg = 'https://cdnd.selectdb.com/images/logo.svg';
@@ -161,6 +162,7 @@ const config = {
161162
// },
162163
showLastUpdateAuthor: false,
163164
showLastUpdateTime: false,
165+
remarkPlugins: [markdownBoldPlugin],
164166
},
165167
blog: {
166168
blogTitle: 'Apache Doris - Blog | Latest news and events ',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"sass-migrator": "^2.2.1",
4242
"swiper": "^9.0.5",
4343
"tailwindcss": "^3.3.6",
44+
"unist-util-visit": "^5.0.0",
4445
"vitpress-generate-pdf": "^1.1.4"
4546
},
4647
"devDependencies": {

0 commit comments

Comments
 (0)