Refactor Markdown pragma related logic (#17950)

This commit is contained in:
fisker Cheung authored and GitHub committed 2025-09-24 05:43:10 +08:00
1 parent 3c8e5aa8e6
commit b35ef6a549
7 files changed
+45 -76

No files matched your search

+5 -10
View File
@@ -5,19 +5,14 @@ import {
} from "../language-js/pragma.js";
import parseFrontMatter from "../utils/front-matter/parse.js";
function hasPragma(text) {
return jsHasPragma(parseFrontMatter(text).content);
}
function hasIgnorePragma(text) {
return jsHasIgnorePragma(parseFrontMatter(text).content);
}
function insertPragma(text) {
const hasPragma = (text) => jsHasPragma(parseFrontMatter(text).content);
const hasIgnorePragma = (text) =>
jsHasIgnorePragma(parseFrontMatter(text).content);
const insertPragma = (text) => {
const { frontMatter, content } = parseFrontMatter(text);
return (
(frontMatter ? frontMatter.raw + "\n\n" : "") + jsInsertPragma(content)
);
}
};
export { hasIgnorePragma, hasPragma, insertPragma };
+3 -11
View File
@@ -4,16 +4,8 @@ import {
GRAPHQL_HAS_PRAGMA_REGEXP,
} from "../utils/pragma/pragma.evaluate.js";
function hasPragma(text) {
return GRAPHQL_HAS_PRAGMA_REGEXP.test(text);
}
function hasIgnorePragma(text) {
return GRAPHQL_HAS_IGNORE_PRAGMA_REGEXP.test(text);
}
function insertPragma(text) {
return `# @${FORMAT_PRAGMA_TO_INSERT}\n\n${text}`;
}
const hasPragma = (text) => GRAPHQL_HAS_PRAGMA_REGEXP.test(text);
const hasIgnorePragma = (text) => GRAPHQL_HAS_IGNORE_PRAGMA_REGEXP.test(text);
const insertPragma = (text) => `# @${FORMAT_PRAGMA_TO_INSERT}\n\n${text}`;
export { hasIgnorePragma, hasPragma, insertPragma };
+4 -11
View File
@@ -4,16 +4,9 @@ import {
HTML_HAS_PRAGMA_REGEXP,
} from "../utils/pragma/pragma.evaluate.js";
function hasPragma(text) {
return HTML_HAS_PRAGMA_REGEXP.test(text);
}
function hasIgnorePragma(text) {
return HTML_HAS_IGNORE_PRAGMA_REGEXP.test(text);
}
function insertPragma(text) {
return `<!-- @${FORMAT_PRAGMA_TO_INSERT} -->\n\n${text}`;
}
const hasPragma = (text) => HTML_HAS_PRAGMA_REGEXP.test(text);
const hasIgnorePragma = (text) => HTML_HAS_IGNORE_PRAGMA_REGEXP.test(text);
const insertPragma = (text) =>
`<!-- @${FORMAT_PRAGMA_TO_INSERT} -->\n\n${text}`;
export { hasIgnorePragma, hasPragma, insertPragma };
+2 -3
View File
@@ -1,8 +1,7 @@
import collapseWhiteSpace from "collapse-white-space";
import cleanFrontMatter from "../utils/front-matter/clean.js";
import isFrontMatter from "../utils/front-matter/is-front-matter.js";
import { FORMAT_PRAGMAS } from "../utils/pragma/pragma.evaluate.js";
import { startWithPragma } from "./pragma.js";
import { hasPragma } from "./pragma.js";
const ignoredProperties = new Set([
"position",
@@ -80,7 +79,7 @@ function clean(original, cloned, parent) {
(parent.children[0] === original ||
(isFrontMatter(parent.children[0]) && parent.children[1] === original)) &&
original.type === "html" &&
startWithPragma(original.value, FORMAT_PRAGMAS)
hasPragma(original.value)
) {
return null;
}
+12 -26
View File
@@ -1,39 +1,25 @@
import parseFrontMatter from "../utils/front-matter/parse.js";
import {
FORMAT_IGNORE_PRAGMAS,
FORMAT_PRAGMA_TO_INSERT,
FORMAT_PRAGMAS,
MARKDOWN_HAS_IGNORE_PRAGMA_REGEXP,
MARKDOWN_HAS_PRAGMA_REGEXP,
} from "../utils/pragma/pragma.evaluate.js";
function startWithPragma(text, pragmas) {
const pragma = `@(${pragmas.join("|")})`;
const regex = new RegExp(
[
`<!--\\s*${pragma}\\s*-->`,
`\\{\\s*\\/\\*\\s*${pragma}\\s*\\*\\/\\s*\\}`,
`<!--.*\r?\n[\\s\\S]*(^|\n)[^\\S\n]*${pragma}[^\\S\n]*($|\n)[\\s\\S]*\n.*-->`,
].join("|"),
"mu",
);
const matched = text.match(regex);
return matched?.index === 0;
}
const hasPragma = (text) =>
startWithPragma(parseFrontMatter(text).content.trimStart(), FORMAT_PRAGMAS);
parseFrontMatter(text).content.trimStart().match(MARKDOWN_HAS_PRAGMA_REGEXP)
?.index === 0;
const hasIgnorePragma = (text) =>
startWithPragma(
parseFrontMatter(text).content.trimStart(),
FORMAT_IGNORE_PRAGMAS,
);
parseFrontMatter(text)
.content.trimStart()
.match(MARKDOWN_HAS_IGNORE_PRAGMA_REGEXP)?.index === 0;
const insertPragma = (text) => {
const extracted = parseFrontMatter(text);
const { frontMatter } = parseFrontMatter(text);
const pragma = `<!-- @${FORMAT_PRAGMA_TO_INSERT} -->`;
return extracted.frontMatter
? `${extracted.frontMatter.raw}\n\n${pragma}\n\n${extracted.content}`
: `${pragma}\n\n${extracted.content}`;
return frontMatter
? `${frontMatter.raw}\n\n${pragma}\n\n${text.slice(frontMatter.end.index)}`
: `${pragma}\n\n${text}`;
};
export { hasIgnorePragma, hasPragma, insertPragma, startWithPragma };
export { hasIgnorePragma, hasPragma, insertPragma };
+4 -15
View File
@@ -5,20 +5,9 @@ import {
YAML_IS_PRAGMA_REGEXP,
} from "../utils/pragma/pragma.evaluate.js";
function isPragma(text) {
return YAML_IS_PRAGMA_REGEXP.test(text);
}
function hasPragma(text) {
return YAML_HAS_PRAGMA_REGEXP.test(text);
}
function hasIgnorePragma(text) {
return YAML_HAS_IGNORE_PRAGMA_REGEXP.test(text);
}
function insertPragma(text) {
return `# @${FORMAT_PRAGMA_TO_INSERT}\n\n${text}`;
}
const isPragma = (text) => YAML_IS_PRAGMA_REGEXP.test(text);
const hasPragma = (text) => YAML_HAS_PRAGMA_REGEXP.test(text);
const hasIgnorePragma = (text) => YAML_HAS_IGNORE_PRAGMA_REGEXP.test(text);
const insertPragma = (text) => `# @${FORMAT_PRAGMA_TO_INSERT}\n\n${text}`;
export { hasIgnorePragma, hasPragma, insertPragma, isPragma };
+15
View File
@@ -40,3 +40,18 @@ export const [HTML_HAS_PRAGMA_REGEXP, HTML_HAS_IGNORE_PRAGMA_REGEXP] = [
(pragmas) =>
new RegExp(String.raw`^\s*<!--\s*@(?:${pragmas.join("|")})\s*-->`, "u"),
);
export const [MARKDOWN_HAS_PRAGMA_REGEXP, MARKDOWN_HAS_IGNORE_PRAGMA_REGEXP] = [
FORMAT_PRAGMAS,
FORMAT_IGNORE_PRAGMAS,
].map((pragmas) => {
const pragma = `@(?:${pragmas.join("|")})`;
return new RegExp(
[
`<!--\\s*${pragma}\\s*-->`,
`\\{\\s*\\/\\*\\s*${pragma}\\s*\\*\\/\\s*\\}`,
`<!--.*\r?\n[\\s\\S]*(^|\n)[^\\S\n]*${pragma}[^\\S\n]*($|\n)[\\s\\S]*\n.*-->`,
].join("|"),
"mu",
);
});