forked from apache/doris-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix]:Fixed the problem of invalid bolding in markdown documents (apa…
…che#1679) Co-authored-by: liyang <liyang@selectdb.com>
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters