refactor(markdown): improve getMinNotPresentContinuousCount (#18138)

This commit is contained in:
Tatsunori Uchino authored and GitHub committed 2025-10-29 02:30:50 +08:00
1 parent 011791fd6c
commit 3df17ca1bb
2 files changed
+7 -3

No files matched your search

+1 -1
View File
@@ -182,7 +182,7 @@ function genericPrint(path, options, print) {
? node.value
: node.value.replaceAll("\n", " ");
const backtickCount = getMinNotPresentContinuousCount(code, "`");
const backtickString = "`".repeat(backtickCount || 1);
const backtickString = "`".repeat(backtickCount);
const padding =
code.startsWith("`") ||
code.endsWith("`") ||
@@ -1,9 +1,13 @@
import escapeStringRegexp from "escape-string-regexp";
/**
* Calculates the minimum `n` (>= 1) where `searchString.repeat(n)` is not present in `text`.
* @param {string} text
* @param {string} searchString
* @returns {number}
* @example getMinNotPresentContinuousCount("```", "`") // 1
* @example getMinNotPresentContinuousCount("``` `", "`") // 2 (1 is occupied)
* @example getMinNotPresentContinuousCount("``` ` ``", "`") // 4 (1-3 are occupied)
* @returns {number} 1 if not exists, see the above example otherwise
*/
function getMinNotPresentContinuousCount(text, searchString) {
const matches = text.match(
@@ -11,7 +15,7 @@ function getMinNotPresentContinuousCount(text, searchString) {
);
if (matches === null) {
return 0;
return 1;
}
const countPresent = new Map();