Markdown: Remove loose-item plugin (#14372)
This commit is contained in:
1 parent
5ea9a8f81f
commit
be474c7891
4 files changed
+27
-56
No files matched your search
@@ -25,7 +25,6 @@ function clean(ast, newObj, parent) {
|
||||
|
||||
if (ast.type === "list" || ast.type === "listItem") {
|
||||
delete newObj.spread;
|
||||
delete newObj.loose;
|
||||
}
|
||||
|
||||
// texts can be splitted or merged
|
||||
|
||||
@@ -9,7 +9,6 @@ import htmlToJsx from "./unified-plugins/html-to-jsx.js";
|
||||
import frontMatter from "./unified-plugins/front-matter.js";
|
||||
import liquid from "./unified-plugins/liquid.js";
|
||||
import wikiLink from "./unified-plugins/wiki-link.js";
|
||||
import looseItems from "./unified-plugins/loose-items.js";
|
||||
|
||||
/**
|
||||
* based on [MDAST](https://github.com/syntax-tree/mdast) with following modifications:
|
||||
@@ -38,8 +37,7 @@ function createParse({ isMDX }) {
|
||||
.use(isMDX ? esSyntax : identity)
|
||||
.use(liquid)
|
||||
.use(isMDX ? htmlToJsx : identity)
|
||||
.use(wikiLink)
|
||||
.use(looseItems);
|
||||
.use(wikiLink);
|
||||
return processor.run(processor.parse(text));
|
||||
};
|
||||
}
|
||||
|
||||
@@ -651,7 +651,7 @@ function printChildren(path, options, print, events = {}) {
|
||||
parts.push(hardline);
|
||||
|
||||
if (
|
||||
shouldPrePrintDoubleHardline(path) ||
|
||||
shouldPrePrintDoubleHardline(path, options) ||
|
||||
shouldPrePrintTripleHardline(path)
|
||||
) {
|
||||
parts.push(hardline);
|
||||
@@ -720,37 +720,44 @@ function shouldPrePrintHardline({ node, parent }) {
|
||||
return !isInlineNode && !isInlineHTML;
|
||||
}
|
||||
|
||||
function shouldPrePrintDoubleHardline({ node, previous, parent }) {
|
||||
function isLooseListItem(node, options) {
|
||||
return (
|
||||
node.type === "listItem" &&
|
||||
(node.spread ||
|
||||
// Check if `listItem` ends with `\n`
|
||||
// since it can't be empty, so we only need check the last character
|
||||
options.originalText.charAt(node.position.end.offset - 1) === "\n")
|
||||
);
|
||||
}
|
||||
|
||||
function shouldPrePrintDoubleHardline({ node, previous, parent }, options) {
|
||||
const isPrevNodeLooseListItem = isLooseListItem(previous, options);
|
||||
|
||||
if (isPrevNodeLooseListItem) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const isSequence = previous.type === node.type;
|
||||
const isSiblingNode = isSequence && SIBLING_NODE_TYPES.has(node.type);
|
||||
|
||||
const isInTightListItem = parent.type === "listItem" && !parent.loose;
|
||||
|
||||
const isPrevNodeLooseListItem =
|
||||
previous.type === "listItem" && previous.loose;
|
||||
|
||||
const isInTightListItem =
|
||||
parent.type === "listItem" && !isLooseListItem(parent, options);
|
||||
const isPrevNodePrettierIgnore = isPrettierIgnore(previous) === "next";
|
||||
|
||||
const isBlockHtmlWithoutBlankLineBetweenPrevHtml =
|
||||
node.type === "html" &&
|
||||
previous.type === "html" &&
|
||||
previous.position.end.line + 1 === node.position.start.line;
|
||||
|
||||
const isHtmlDirectAfterListItem =
|
||||
node.type === "html" &&
|
||||
parent.type === "listItem" &&
|
||||
previous.type === "paragraph" &&
|
||||
previous.position.end.line + 1 === node.position.start.line;
|
||||
|
||||
return (
|
||||
isPrevNodeLooseListItem ||
|
||||
!(
|
||||
isSiblingNode ||
|
||||
isInTightListItem ||
|
||||
isPrevNodePrettierIgnore ||
|
||||
isBlockHtmlWithoutBlankLineBetweenPrevHtml ||
|
||||
isHtmlDirectAfterListItem
|
||||
)
|
||||
return !(
|
||||
isSiblingNode ||
|
||||
isInTightListItem ||
|
||||
isPrevNodePrettierIgnore ||
|
||||
isBlockHtmlWithoutBlankLineBetweenPrevHtml ||
|
||||
isHtmlDirectAfterListItem
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
function looseItems() {
|
||||
const proto = this.Parser.prototype;
|
||||
const originalList = proto.blockTokenizers.list;
|
||||
|
||||
function fixListNodes(value, node, parent) {
|
||||
if (node.type === "listItem") {
|
||||
node.loose = node.spread || value.endsWith("\n");
|
||||
if (node.loose) {
|
||||
parent.loose = true;
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
proto.blockTokenizers.list = function list(realEat, value, silent) {
|
||||
function eat(subvalue) {
|
||||
const realAdd = realEat(subvalue);
|
||||
|
||||
function add(node, parent) {
|
||||
return realAdd(fixListNodes(subvalue, node, parent), parent);
|
||||
}
|
||||
add.reset = function (node, parent) {
|
||||
return realAdd.reset(fixListNodes(subvalue, node, parent), parent);
|
||||
};
|
||||
|
||||
return add;
|
||||
}
|
||||
eat.now = realEat.now;
|
||||
return originalList.call(this, eat, value, silent);
|
||||
};
|
||||
}
|
||||
|
||||
export default looseItems;
|
||||
Reference in new issue
Block a user