feat(markdown): support top-level `prettier-ignore-start/end` (#4202)
* test: add tests * feat(markdown): support top-level `prettier-ignore-start/end` * docs(ignore): add range ignoremaster
parent
f23ce27856
commit
1b09fde361
|
@ -69,3 +69,24 @@ matrix(
|
|||
<!-- prettier-ignore -->
|
||||
Do not format this
|
||||
```
|
||||
|
||||
### Range Ignore
|
||||
|
||||
_available in v1.12.0+_
|
||||
|
||||
This type of ignore is only allowed to be used in top-level and aimed to disable formatting for auto-generated content, e.g. [`all-contributors`](https://github.com/kentcdodds/all-contributors), [`markdown-toc`](https://github.com/jonschlinkert/markdown-toc), etc.
|
||||
|
||||
<!-- TODO: remove this ignore when upgraded to v1.12 -->
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```markdown
|
||||
<!-- prettier-ignore-start -->
|
||||
<!-- SOMETHING AUTO-GENERATED BY TOOLS - START -->
|
||||
|
||||
| MY | AWESOME | AUTO-GENERATED | TABLE |
|
||||
|-|-|-|-|
|
||||
| a | b | c | d |
|
||||
|
||||
<!-- SOMETHING AUTO-GENERATED BY TOOLS - END -->
|
||||
<!-- prettier-ignore-end -->
|
||||
```
|
||||
|
|
|
@ -64,10 +64,7 @@ function genericPrint(path, options, print) {
|
|||
|
||||
switch (node.type) {
|
||||
case "root":
|
||||
return concat([
|
||||
normalizeDoc(printChildren(path, options, print)),
|
||||
hardline
|
||||
]);
|
||||
return concat([normalizeDoc(printRoot(path, options, print)), hardline]);
|
||||
case "paragraph":
|
||||
return printChildren(path, options, print, {
|
||||
postprocessor: fill
|
||||
|
@ -533,6 +530,68 @@ function printTable(path, options, print) {
|
|||
}
|
||||
}
|
||||
|
||||
function printRoot(path, options, print) {
|
||||
/** @typedef {{ index: number, offset: number }} IgnorePosition */
|
||||
/** @type {Array<{start: IgnorePosition, end: IgnorePosition}>} */
|
||||
const ignoreRanges = [];
|
||||
|
||||
/** @type {IgnorePosition | null} */
|
||||
let ignoreStart = null;
|
||||
|
||||
const children = path.getValue().children;
|
||||
children.forEach((childNode, index) => {
|
||||
switch (isPrettierIgnore(childNode)) {
|
||||
case "start":
|
||||
if (ignoreStart === null) {
|
||||
ignoreStart = { index, offset: childNode.position.end.offset };
|
||||
}
|
||||
break;
|
||||
case "end":
|
||||
if (ignoreStart !== null) {
|
||||
ignoreRanges.push({
|
||||
start: ignoreStart,
|
||||
end: { index, offset: childNode.position.start.offset }
|
||||
});
|
||||
ignoreStart = null;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// do nothing
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
return printChildren(path, options, print, {
|
||||
processor: (childPath, index) => {
|
||||
if (ignoreRanges.length !== 0) {
|
||||
const ignoreRange = ignoreRanges[0];
|
||||
|
||||
if (index === ignoreRange.start.index) {
|
||||
return concat([
|
||||
children[ignoreRange.start.index].value,
|
||||
options.originalText.slice(
|
||||
ignoreRange.start.offset,
|
||||
ignoreRange.end.offset
|
||||
),
|
||||
children[ignoreRange.end.index].value
|
||||
]);
|
||||
}
|
||||
|
||||
if (ignoreRange.start.index < index && index < ignoreRange.end.index) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (index === ignoreRange.end.index) {
|
||||
ignoreRanges.shift();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return childPath.call(print);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function printChildren(path, options, print, events) {
|
||||
events = events || {};
|
||||
|
||||
|
@ -559,7 +618,7 @@ function printChildren(path, options, print, events) {
|
|||
prettierIgnore = false;
|
||||
|
||||
if (result !== false) {
|
||||
prettierIgnore = isPrettierIgnore(childNode);
|
||||
prettierIgnore = isPrettierIgnore(childNode) === "next";
|
||||
|
||||
const data = {
|
||||
parts,
|
||||
|
@ -593,10 +652,15 @@ function printChildren(path, options, print, events) {
|
|||
return postprocessor(parts);
|
||||
}
|
||||
|
||||
/** @return {false | 'next' | 'start' | 'end'} */
|
||||
function isPrettierIgnore(node) {
|
||||
return (
|
||||
node.type === "html" && /^<!--\s*prettier-ignore\s*-->$/.test(node.value)
|
||||
if (node.type !== "html") {
|
||||
return false;
|
||||
}
|
||||
const match = node.value.match(
|
||||
/^<!--\s*prettier-ignore(?:-(start|end))?\s*-->$/
|
||||
);
|
||||
return match === null ? false : match[1] ? match[1] : "next";
|
||||
}
|
||||
|
||||
function shouldNotPrePrintHardline(node, data) {
|
||||
|
@ -621,7 +685,7 @@ function shouldPrePrintDoubleHardline(node, data) {
|
|||
const isPrevNodeLooseListItem =
|
||||
data.prevNode && data.prevNode.type === "listItem" && data.prevNode.loose;
|
||||
|
||||
const isPrevNodePrettierIgnore = isPrettierIgnore(data.prevNode);
|
||||
const isPrevNodePrettierIgnore = isPrettierIgnore(data.prevNode) === "next";
|
||||
|
||||
return (
|
||||
isPrevNodeLooseListItem ||
|
||||
|
|
|
@ -27,3 +27,52 @@ This is a long long long long long long long long long long long long long long
|
|||
This is a long long long long long long long long long long long long long long long paragraph.
|
||||
|
||||
`;
|
||||
|
||||
exports[`top-level-range.md 1`] = `
|
||||
<!-- prettier-ignore-start -->
|
||||
<!-- some tool start (this should be ignored) -->
|
||||
|
||||
| some | table |
|
||||
| - | - |
|
||||
| 1 | a |
|
||||
| 2 | b |
|
||||
|
||||
<!-- some tool end -->
|
||||
<!-- prettier-ignore-end -->
|
||||
|
||||
> <!-- prettier-ignore-start -->
|
||||
> <!-- some tool start (this shouldn't be ignored) -->
|
||||
>
|
||||
> | some | table |
|
||||
> | - | - |
|
||||
> | 1 | a |
|
||||
> | 2 | b |
|
||||
>
|
||||
> <!-- some tool end -->
|
||||
> <!-- prettier-ignore-end -->
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
<!-- prettier-ignore-start -->
|
||||
<!-- some tool start (this should be ignored) -->
|
||||
|
||||
| some | table |
|
||||
| - | - |
|
||||
| 1 | a |
|
||||
| 2 | b |
|
||||
|
||||
<!-- some tool end -->
|
||||
<!-- prettier-ignore-end -->
|
||||
|
||||
> <!-- prettier-ignore-start -->
|
||||
>
|
||||
> <!-- some tool start (this shouldn't be ignored) -->
|
||||
>
|
||||
> | some | table |
|
||||
> | ---- | ----- |
|
||||
> | 1 | a |
|
||||
> | 2 | b |
|
||||
>
|
||||
> <!-- some tool end -->
|
||||
>
|
||||
> <!-- prettier-ignore-end -->
|
||||
|
||||
`;
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
<!-- prettier-ignore-start -->
|
||||
<!-- some tool start (this should be ignored) -->
|
||||
|
||||
| some | table |
|
||||
| - | - |
|
||||
| 1 | a |
|
||||
| 2 | b |
|
||||
|
||||
<!-- some tool end -->
|
||||
<!-- prettier-ignore-end -->
|
||||
|
||||
> <!-- prettier-ignore-start -->
|
||||
> <!-- some tool start (this shouldn't be ignored) -->
|
||||
>
|
||||
> | some | table |
|
||||
> | - | - |
|
||||
> | 1 | a |
|
||||
> | 2 | b |
|
||||
>
|
||||
> <!-- some tool end -->
|
||||
> <!-- prettier-ignore-end -->
|
Loading…
Reference in New Issue