Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 3 Bugs and Add a New NPM Command #530

Merged
merged 6 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions __tests__/consecutive-blank-lines.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,104 @@ ruleTest({
\`\`\`
`,
},
{ // accounts for https://github.com/platers/obsidian-linter/issues/529
testName: 'Removes multiple empty lines when one of the lines just has spaces in it',
before: dedent`
Line 1
${''}
${' '}
Line 2
`,
after: dedent`
Line 1
${''}
Line 2
`,
},
{ // accounts for https://github.com/platers/obsidian-linter/issues/529
testName: 'Removes multiple empty lines when one of the lines just has a tab in it',
before: dedent`
Line 1
${''}
${''}\t
Line 2
`,
after: dedent`
Line 1
${''}
Line 2
`,
},
{ // accounts for https://github.com/platers/obsidian-linter/issues/529
testName: 'Removes multiple empty lines when one of the lines just has a carriage return in it',
before: dedent`
Line 1
${''}
${''}\r
${''}\v
Line 2
`,
after: dedent`
Line 1
${''}
Line 2
`,
},
{ // accounts for https://github.com/platers/obsidian-linter/issues/529
testName: 'Removes multiple empty lines when one of the lines just has a vertical tab in it',
before: dedent`
Line 1
${''}
${''}\v
Line 2
`,
after: dedent`
Line 1
${''}
Line 2
`,
},
{ // accounts for https://github.com/platers/obsidian-linter/issues/529
testName: 'Removes multiple empty lines when one of the lines just has a page break in it',
before: dedent`
Line 1
${''}
${''}\f
Line 2
`,
after: dedent`
Line 1
${''}
Line 2
`,
},
{ // accounts for https://github.com/platers/obsidian-linter/issues/529
testName: 'Removes multiple empty lines when one of the lines just has a page break in it',
before: dedent`
Line 1
${''}
${''}\f
Line 2
`,
after: dedent`
Line 1
${''}
Line 2
`,
},
{ // accounts for https://github.com/platers/obsidian-linter/issues/529
testName: 'Removes multiple empty lines when one of the lines just has multiple kinds of whitespace in it in it',
before: dedent`
Line 1
${''}
${''}\f \r\v\t\t\v
Line 2
`,
after: dedent`
Line 1
${''}
Line 2
`,
},
],
});
31 changes: 31 additions & 0 deletions __tests__/paragraph-blank-lines.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,5 +265,36 @@ ruleTest({
%%
`,
},
{ // accounts for https://github.com/platers/obsidian-linter/issues/517
testName: 'Table followed by header should only have 1 line after it',
before: dedent`
### 常量

| \`[[Link]]\` | A link to the file named "Link" |
|:--------------------|:----------------------------------|
| \`[[Link]]\` | A link to the file named "Link" |
| \`[1, 2, 3]\` | A list of numbers 1, 2, and 3 |
| \`[[1, 2],[3, 4]]\` | A list of lists |
| \`{ a: 1, b: 2 }\` | An object |
| \`date()\` | |
| \`dur()\` | |

### 表达式
`,
after: dedent`
### 常量

| \`[[Link]]\` | A link to the file named "Link" |
|:--------------------|:----------------------------------|
| \`[[Link]]\` | A link to the file named "Link" |
| \`[1, 2, 3]\` | A list of numbers 1, 2, and 3 |
| \`[[1, 2],[3, 4]]\` | A list of lists |
| \`{ a: 1, b: 2 }\` | An object |
| \`date()\` | |
| \`dur()\` | |

### 表达式
`,
},
],
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"test-suite": "jest -t \"$1\"",
"docs": "node docs.js",
"compile": "npm run build && npm run docs && npm run lint && npm run test",
"lint": "eslint . --ext .ts --fix"
"lint": "eslint . --ext .ts --fix",
"clear-jest": "jest --clearCache"
},
"keywords": [],
"author": "",
Expand Down
3 changes: 2 additions & 1 deletion src/rules/consecutive-blank-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export default class ConsecutiveBlankLines extends RuleBuilder<ConsecutiveBlankL
}
apply(text: string, options: ConsecutiveBlankLinesOptions): string {
return ignoreListOfTypes([IgnoreTypes.code, IgnoreTypes.yaml, IgnoreTypes.link, IgnoreTypes.wikiLink, IgnoreTypes.tag], text, (text) => {
return text.replace(/\n{2,}/g, '\n\n');
// make sure to account for lines that are purely whitespace as well https://stackoverflow.com/a/3873354/8353749
return text.replace(/(\n([\t\v\f\r \u00a0\u2000-\u200b\u2028-\u2029\u3000]+)?){2,}/g, '\n\n');
});
}
get exampleBuilders(): ExampleBuilder<ConsecutiveBlankLinesOptions>[] {
Expand Down
19 changes: 15 additions & 4 deletions src/rules/footnote-after-punctuation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ export default class FootnoteAfterPunctuation extends RuleBuilder<FootnoteAfterP
return RuleType.FOOTNOTE;
}
apply(text: string, options: FootnoteAfterPunctuationOptions): string {
return ignoreListOfTypes([IgnoreTypes.code, IgnoreTypes.yaml, IgnoreTypes.link, IgnoreTypes.wikiLink, IgnoreTypes.tag], text, (text) => {
// regex uses hack to treat lookahead as lookaround https://stackoverflow.com/a/43232659
// needed to ensure that no footnote text followed by ":" is matched
return text.replace(/(?!^)(\[\^\w+\]) ?([,.;!:?])/gm, '$2$1');
return ignoreListOfTypes([IgnoreTypes.code, IgnoreTypes.yaml, IgnoreTypes.link, IgnoreTypes.wikiLink, IgnoreTypes.tag, IgnoreTypes.footnoteAtStartOfLine, IgnoreTypes.footnoteAfterATask], text, (text) => {
return text.replace(/(\[\^\w+\]) ?([,.;!:?])/gm, '$2$1');
});
}
get exampleBuilders(): ExampleBuilder<FootnoteAfterPunctuationOptions>[] {
Expand All @@ -38,6 +36,19 @@ export default class FootnoteAfterPunctuation extends RuleBuilder<FootnoteAfterP
Lorem.[^1] Ipsum,[^2] doletes.
`,
}),
new ExampleBuilder({
description: 'A footnote at the start of a task is not moved to after the punctuation',
before: dedent`
- [ ] [^1]: This is a footnote and a task.
- [ ] This is a footnote and a task that gets swapped with the punctuation[^2]!
[^2]: This footnote got modified
`,
after: dedent`
- [ ] [^1]: This is a footnote and a task.
- [ ] This is a footnote and a task that gets swapped with the punctuation![^2]
[^2]: This footnote got modified
`,
}),
];
}
get optionBuilders(): OptionBuilderBase<FootnoteAfterPunctuationOptions>[] {
Expand Down
2 changes: 2 additions & 0 deletions src/utils/ignore-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export const IgnoreTypes: Record<string, IgnoreType> = {
tag: {replaceAction: tagRegex, placeholder: '#tag-placeholder'},
obsidianMultiLineComments: {replaceAction: obsidianMultilineCommentRegex, placeholder: '{OBSIDIAN_COMMENT_PLACEHOLDER}'},
table: {replaceAction: tableRegex, placeholder: '{TABLE_PLACEHOLDER}'},
footnoteAtStartOfLine: {replaceAction: /^(\[\^\w+\]) ?([,.;!:?])/gm, placeholder: '{FOOTNOTE_AT_START_OF_LINE_PLACEHOLDER}'},
footnoteAfterATask: {replaceAction: /- \[.] (\[\^\w+\]) ?([,.;!:?])/gm, placeholder: '{FOOTNOTE_AFTER_A_TASK_PLACEHOLDER}'},
// custom functions
link: {replaceAction: replaceMarkdownLinks, placeholder: '{REGULAR_LINK_PLACEHOLDER}'},
} as const;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const ellipsisRegex = /(\. ?){2}\./g;
export const lineStartingWithWhitespaceOrBlockquoteTemplate = `\\s*(>\\s*)*`;
// Note that the following regex has an issue where if the table is followed by another table with only 1 blank line between them, it considers them to be one table
// if this becomes an issue, we can address it then
export const tableRegex = /((((>[ ]?)*)|([ ]{0,3}))\[.*?\][ \t]*\n)?((((>[ ]?)*)|([ ]{0,3}))\S+.*?\|.*?\n([^\n]*?\|[^\n]*?\n)*?)?(((>[ ]?)*)|([ ]{0,3}))[|\-+:.][ \-+|:.]*?\|[ \-+|:.]*(?:\n?[^\n]*?\|([^\n]*?)*(\n)?)+/g;
export const tableRegex = /((((>[ ]?)*)|([ ]{0,3}))\[.*?\][ \t]*\n)?((((>[ ]?)*)|([ ]{0,3}))\S+.*?\|.*?\n([^\n]*?\|[^\n]*?\n)*?)?(((>[ ]?)*)|([ ]{0,3}))[|\-+:.][ \-+|:.]*?\|[ \-+|:.]*(?:\n?[^\n]*?\|([^\n]*?)*)+/g;
export const urlRegex = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s`\]'"‘’“”>]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s`\]'"‘’“”>]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s`\]'"‘’“”>]{2,}|www\.[a-zA-Z0-9]+\.[^\s`\]'"‘’“”>]{2,})/gi;

// https://stackoverflow.com/questions/38866071/javascript-replace-method-dollar-signs
Expand Down Expand Up @@ -70,7 +70,7 @@ export function ensureEmptyLinesAroundTables(text: string): string {

for (const table of tableMatches) {
let start = text.indexOf(table);
const end = start + table.trimEnd().length;
const end = start + table.length;
if (table.trim().startsWith('>')) {
while (text.charAt(start).trim() === '' || text.charAt(start) === '>') {
start++;
Expand Down