Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Feb 6, 2025
1 parent c71e298 commit febffbc
Show file tree
Hide file tree
Showing 7 changed files with 698 additions and 677 deletions.
13 changes: 3 additions & 10 deletions doc-build/md038.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,12 @@ Note: Code spans containing only spaces are allowed by the specification:
` ` or ` `
```

Note: A single leading and trailing space is allowed by the specification and
Note: A single leading *and* trailing space is allowed by the specification and
automatically trimmed by the parser (in order to allow for code spans that embed
backticks):
one or more backticks):

```markdown
`` `backticks` ``
```

Note: A single leading or trailing space is allowed if used to separate code
span markers from an embedded backtick (though the space is not trimmed):

```markdown
`` ` embedded backtick``
`` `backticks` `` or `` backtick` ``
```

Rationale: Violations of this rule are usually unintentional and may lead to
Expand Down
13 changes: 3 additions & 10 deletions doc/Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -1549,19 +1549,12 @@ Note: Code spans containing only spaces are allowed by the specification:
` ` or ` `
```

Note: A single leading and trailing space is allowed by the specification and
Note: A single leading *and* trailing space is allowed by the specification and
automatically trimmed by the parser (in order to allow for code spans that embed
backticks):
one or more backticks):

```markdown
`` `backticks` ``
```

Note: A single leading or trailing space is allowed if used to separate code
span markers from an embedded backtick (though the space is not trimmed):

```markdown
`` ` embedded backtick``
`` `backticks` `` or `` backtick` ``
```

Rationale: Violations of this rule are usually unintentional and may lead to
Expand Down
13 changes: 3 additions & 10 deletions doc/md038.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,12 @@ Note: Code spans containing only spaces are allowed by the specification:
` ` or ` `
```

Note: A single leading and trailing space is allowed by the specification and
Note: A single leading *and* trailing space is allowed by the specification and
automatically trimmed by the parser (in order to allow for code spans that embed
backticks):
one or more backticks):

```markdown
`` `backticks` ``
```

Note: A single leading or trailing space is allowed if used to separate code
span markers from an embedded backtick (though the space is not trimmed):

```markdown
`` ` embedded backtick``
`` `backticks` `` or `` backtick` ``
```

Rationale: Violations of this rule are usually unintentional and may lead to
Expand Down
96 changes: 33 additions & 63 deletions lib/md038.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,9 @@ import { addErrorContext } from "../helpers/helpers.cjs";
import { getDescendantsByType } from "../helpers/micromark-helpers.cjs";
import { filterByTypesCached } from "./cache.mjs";

const leftSpaceRe = /^\s(?:[^`]|$)/;
const rightSpaceRe = /[^`]\s$/;
const startSpaceRe = /^\s+(?!`)|^\s+(?=\s`)/;
const endSpaceRe = /(?<!`)\s+$|(?<=`\s)\s+$/;
const allSpaceRe = /^\s*$/;
const trimCodeText = (text, start, end) => {
text = text.replace(/^\s+$/, "");
if (start) {
text = text.replace(/^\s+?(\s`|\S)/, "$1");
}
if (end) {
text = text.replace(/(`\s|\S)\s+$/, "$1");
}
return text;
};

/** @type {import("markdownlint").Rule} */
export default {
Expand All @@ -27,66 +17,46 @@ export default {
"function": function MD038(params, onError) {
const codeTexts = filterByTypesCached([ "codeText" ]);
for (const codeText of codeTexts) {
const sequences = getDescendantsByType(codeText, [ "codeTextSequence" ]);
const startSequence = sequences[0];
const endSequence = sequences[sequences.length - 1];
const datas = getDescendantsByType(codeText, [ "codeTextData" ]);
const startData = datas[0];
const endData = datas[datas.length - 1];
if (startSequence && endSequence && startData && endData) {
const spaceLeft = leftSpaceRe.test(startData.text);
const spaceRight = rightSpaceRe.test(endData.text);
if (startData && endData) {
const startMatch = startSpaceRe.exec(startData.text);
const endMatch = endSpaceRe.exec(endData.text);
if (
(spaceLeft || spaceRight) &&
(startMatch || endMatch) &&
!datas.every((data) => allSpaceRe.test(data.text))
) {
let lineNumber = startSequence.startLine;
let range = undefined;
let fixInfo = undefined;
if (startSequence.startLine === endSequence.endLine) {
range = [
startSequence.startColumn,
endSequence.endColumn - startSequence.startColumn
];
fixInfo = {
"editColumn": startSequence.endColumn,
"deleteCount": endSequence.startColumn - startSequence.endColumn,
"insertText": trimCodeText(startData.text, true, true)
};
} else if (spaceLeft && (startSequence.endLine === startData.startLine)) {
range = [
startSequence.startColumn,
startData.endColumn - startSequence.startColumn
];
fixInfo = {
"editColumn": startSequence.endColumn,
"deleteCount": startData.endColumn - startData.startColumn,
"insertText": trimCodeText(startData.text, true, false)
};
} else if (spaceRight && (endData.text.trim().length > 0)) {
lineNumber = endSequence.endLine;
range = [
endData.startColumn,
endSequence.endColumn - endData.startColumn
];
fixInfo = {
"editColumn": endData.startColumn,
"deleteCount": endData.endColumn - endData.startColumn,
"insertText": trimCodeText(endData.text, false, true)
};
if (startMatch) {
const { startColumn, startLine, text } = startData;
const startSpaces = startMatch[0].length;
addErrorContext(
onError,
startLine,
text,
true,
false,
[ startColumn, startSpaces ],
{
"editColumn": startColumn,
"deleteCount": startSpaces
}
);
}
if (range) {
const context = params
.lines[lineNumber - 1]
.substring(range[0] - 1, range[0] - 1 + range[1]);
if (endMatch) {
const { endColumn, endLine, text } = endData;
const endSpaces = endMatch[0].length;
addErrorContext(
onError,
lineNumber,
context,
spaceLeft,
spaceRight,
range,
fixInfo
endLine,
text,
false,
true,
[ endColumn - endSpaces, endSpaces ],
{
"editColumn": endColumn - endSpaces,
"deleteCount": endSpaces
}
);
}
}
Expand Down
Loading

0 comments on commit febffbc

Please sign in to comment.