From 5b962d6f148795ca9ee30894538dd4e1b84df35b Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sun, 18 Sep 2022 19:25:21 +0800 Subject: [PATCH] Extract `inferParserByLanguage` to seperate file (#13483) --- src/common/util.js | 16 ---------------- src/language-html/parser-html.js | 2 +- src/language-html/utils/index.js | 3 ++- src/language-markdown/embed.js | 6 ++---- src/utils/infer-parser-by-language.js | 17 +++++++++++++++++ 5 files changed, 22 insertions(+), 22 deletions(-) create mode 100644 src/utils/infer-parser-by-language.js diff --git a/src/common/util.js b/src/common/util.js index 4571581d1..d5c686693 100644 --- a/src/common/util.js +++ b/src/common/util.js @@ -1,5 +1,4 @@ import escapeStringRegexp from "escape-string-regexp"; -import { getSupportInfo } from "../main/support.js"; import { skipWhitespace, skipSpaces, @@ -423,20 +422,6 @@ function addTrailingComment(node, comment) { addCommentHelper(node, comment); } -function inferParserByLanguage(language, options) { - const { languages } = getSupportInfo({ plugins: options.plugins }); - const matched = - languages.find(({ name }) => name.toLowerCase() === language) || - languages.find( - ({ aliases }) => Array.isArray(aliases) && aliases.includes(language) - ) || - languages.find( - ({ extensions }) => - Array.isArray(extensions) && extensions.includes(`.${language}`) - ); - return matched && matched.parsers[0]; -} - function isFrontMatterNode(node) { return node && node.type === "front-matter"; } @@ -473,7 +458,6 @@ function describeNodeForDebugging(node) { } export { - inferParserByLanguage, getMaxContinuousCount, getMinNotPresentContinuousCount, getNextNonSpaceNonCommentCharacterIndexWithStartIndex, diff --git a/src/language-html/parser-html.js b/src/language-html/parser-html.js index 1a359e443..86e488cef 100644 --- a/src/language-html/parser-html.js +++ b/src/language-html/parser-html.js @@ -1,6 +1,6 @@ import parseFrontMatter from "../utils/front-matter/parse.js"; +import inferParserByLanguage from "../utils/infer-parser-by-language.js"; import createError from "../common/parser-create-error.js"; -import { inferParserByLanguage } from "../common/util.js"; import HTML_TAGS from "./utils/html-tag-names.js"; import HTML_ELEMENT_ATTRIBUTES from "./utils/html-elements-attributes.js"; import isUnknownNamespace from "./utils/is-unknown-namespace.js"; diff --git a/src/language-html/utils/index.js b/src/language-html/utils/index.js index b60be8204..21bcb030a 100644 --- a/src/language-html/utils/index.js +++ b/src/language-html/utils/index.js @@ -2,7 +2,8 @@ * @typedef {import("../../common/ast-path.js").default} AstPath */ -import { inferParserByLanguage, isFrontMatterNode } from "../../common/util.js"; +import { isFrontMatterNode } from "../../common/util.js"; +import inferParserByLanguage from "../../utils/infer-parser-by-language.js"; import { line, hardline, join } from "../../document/builders.js"; import { replaceEndOfLine } from "../../document/utils.js"; import { diff --git a/src/language-markdown/embed.js b/src/language-markdown/embed.js index a1f429ea1..536ee9284 100644 --- a/src/language-markdown/embed.js +++ b/src/language-markdown/embed.js @@ -1,10 +1,8 @@ -import { - inferParserByLanguage, - getMaxContinuousCount, -} from "../common/util.js"; +import { getMaxContinuousCount } from "../common/util.js"; import { hardline, markAsRoot } from "../document/builders.js"; import { replaceEndOfLine } from "../document/utils.js"; import printFrontMatter from "../utils/front-matter/print.js"; +import inferParserByLanguage from "../utils/infer-parser-by-language.js"; import { getFencedCodeBlockValue } from "./utils.js"; function embed(path, options) { diff --git a/src/utils/infer-parser-by-language.js b/src/utils/infer-parser-by-language.js new file mode 100644 index 000000000..3dcdf7d21 --- /dev/null +++ b/src/utils/infer-parser-by-language.js @@ -0,0 +1,17 @@ +import { getSupportInfo } from "../main/support.js"; + +function inferParserByLanguage(language, options) { + const { languages } = getSupportInfo({ plugins: options.plugins }); + const matched = + languages.find(({ name }) => name.toLowerCase() === language) || + languages.find( + ({ aliases }) => Array.isArray(aliases) && aliases.includes(language) + ) || + languages.find( + ({ extensions }) => + Array.isArray(extensions) && extensions.includes(`.${language}`) + ); + return matched && matched.parsers[0]; +} + +export default inferParserByLanguage;