Visitar URL original
Simplify blockquote marker handling · prettier/prettier@05cd050 · GitHub
Skip to content

Commit 05cd050

Browse files
seiyabcodex
andcommitted
Simplify blockquote marker handling
Co-Authored-By: codex <codex@openai.com>
1 parent da169e0 commit 05cd050

1 file changed

Lines changed: 14 additions & 22 deletions

File tree

‎src/language-markdown/print/preprocess.js‎

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ function splitTextIntoSentences(ast) {
249249
}
250250

251251
function getBlockquoteRawText(text, node) {
252+
const rawBlockquoteMarker = />|\\>|&gt;|&GT;|&#0*62;|&#[xX]0*3[eE];/;
252253
const rawLines = text.split("\n");
253254
const valueLines = node.value.split("\n");
254255
let valueIndex = 0;
@@ -262,37 +263,28 @@ function getBlockquoteRawText(text, node) {
262263
if (index === 0) {
263264
return rawLine;
264265
}
265-
const rawLeadingMatches = leadingMatchPositions(
266-
rawLine,
267-
/>|\\>|&gt;|&GT;|&#0*62;|&#[xX]0*3[eE];/,
268-
);
269-
const valueLeadingMatches = leadingMatchPositions(valueLine, />/);
270-
const matchesToBeRemoved =
271-
rawLeadingMatches.length - valueLeadingMatches.length;
266+
const rawMarkerCount = countLeadingMarkers(rawLine, rawBlockquoteMarker);
267+
const valueMarkerCount = countLeadingMarkers(valueLine, />/);
268+
const matchesToBeRemoved = rawMarkerCount - valueMarkerCount;
272269
if (matchesToBeRemoved <= 0) {
273270
return rawLine;
274271
}
275-
return rawLine.slice(rawLeadingMatches[matchesToBeRemoved - 1].end);
272+
return rawLine.replace(
273+
new RegExp(
274+
String.raw`^[ \t]*(?:(?:${rawBlockquoteMarker.source})[ \t]*){${matchesToBeRemoved}}`,
275+
"u",
276+
),
277+
"",
278+
);
276279
});
277280
return resultLines.join("\n");
278281
}
279282

280-
/**
281-
* @param text {string}
282-
* @param pattern {RegExp}
283-
* @returns {Array<{start: number, end: number}>}
284-
*/
285-
function leadingMatchPositions(text, pattern) {
283+
function countLeadingMarkers(text, marker) {
286284
const [prefix] = text.match(
287-
new RegExp(String.raw`^[ \t]*(?:(?:${pattern.source})[ \t]*)*`, "u"),
288-
);
289-
const matches = prefix.matchAll(
290-
new RegExp(String.raw`(?:${pattern.source})[ \t]*`, "gu"),
285+
new RegExp(String.raw`^[ \t]*(?:(?:${marker.source})[ \t]*)*`, "u"),
291286
);
292-
return [...matches].map(({ 0: match, index }) => ({
293-
start: index,
294-
end: index + match.length,
295-
}));
287+
return prefix.match(new RegExp(marker.source, "gu"))?.length ?? 0;
296288
}
297289

298290
function transformIndentedCodeblock(ast, options) {

0 commit comments

Comments
 (0)