Skip to content

Commit

Permalink
[fix]:Fixed the problem of invalid bolding in markdown documents (apa…
Browse files Browse the repository at this point in the history
…che#1679)

Co-authored-by: liyang <liyang@selectdb.com>
  • Loading branch information
2 people authored and echo-hhj committed Jan 6, 2025
1 parent 1e13d56 commit 3479024
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
73 changes: 73 additions & 0 deletions config/markdown-bold-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const { visit } = require('unist-util-visit');

function getFirstBoldContent(str, startIdx = 0) {
const strArr = str.split("");
for (let i = startIdx; i < strArr.length; i++) {
if (
strArr[i - 1] !== "*" &&
strArr[i] === "*" &&
strArr[i + 1] === "*" &&
strArr[i + 2] !== "*"
) {
// start with **
let j;
for (j = i + 2; j < strArr.length; j++) {
if (
strArr[j - 1] !== "*" &&
strArr[j] === "*" &&
strArr[j + 1] === "*" &&
strArr[j + 2] !== "*"
) {
// end with **
return {
start: i,
end: j,
};
}
}
}
}
return null;
}

const plugin = options => {
const transformer = async (ast, file) => {
visit(ast, 'text', (node, index, parent) => {
if (getFirstBoldContent(node.value)) {
const { start, end } = getFirstBoldContent(node.value);
const value = node.value.slice(start + 2, end);
parent.children[index] = {
type: 'strong',
children: [
{
value: value,
type: 'text',
},
],
};
const [boldBefore, , boldAfter] = node.value.split('**');
let hasBoldBefore = !!boldBefore;
if (boldBefore) {
parent.children.splice(index, 0, {
type: 'text',
value: boldBefore,
});
}
if (boldAfter) {
parent.children.splice(hasBoldBefore ? index + 2 : index + 1, 0, {
type: 'text',
value: boldAfter,
});
}
console.warn(
`The bold syntax of "${value}" in "${file.path}" is invalid and is being automatically optimized`,
);
}
});
};
return transformer;
};

module.exports = {
markdownBoldPlugin: plugin,
};
2 changes: 2 additions & 0 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { ssrTemplate } = require('./config/ssrTemplate');
const customDocusaurusPlugin = require('./config/custom-docusaurus-plugin');
const versionsPlugin = require('./config/versions-plugin');
const VERSIONS = require('./versions.json');
const { markdownBoldPlugin } = require('./config/markdown-bold-plugin');
const lightCodeTheme = themes.dracula;

const logoImg = 'https://cdnd.selectdb.com/images/logo.svg';
Expand Down Expand Up @@ -161,6 +162,7 @@ const config = {
// },
showLastUpdateAuthor: false,
showLastUpdateTime: false,
remarkPlugins: [markdownBoldPlugin],
},
blog: {
blogTitle: 'Apache Doris - Blog | Latest news and events ',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"sass-migrator": "^2.2.1",
"swiper": "^9.0.5",
"tailwindcss": "^3.3.6",
"unist-util-visit": "^5.0.0",
"vitpress-generate-pdf": "^1.1.4"
},
"devDependencies": {
Expand Down

0 comments on commit 3479024

Please sign in to comment.