From 6b6ade82f09680f297db9d40b357721954bf4326 Mon Sep 17 00:00:00 2001 From: Peter Kaufman Date: Wed, 30 Nov 2022 20:57:11 -0500 Subject: [PATCH 1/5] added the logic to clear jest cache as an npm command and logic for ignoring tasks for moving punctuation after the footnote key. --- package.json | 3 ++- src/rules/footnote-after-punctuation.ts | 20 ++++++++++++++++---- src/utils/ignore-types.ts | 2 ++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 4f4dde23..bc767f8e 100644 --- a/package.json +++ b/package.json @@ -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": "", diff --git a/src/rules/footnote-after-punctuation.ts b/src/rules/footnote-after-punctuation.ts index fb3e9cb6..9cb3af8e 100644 --- a/src/rules/footnote-after-punctuation.ts +++ b/src/rules/footnote-after-punctuation.ts @@ -21,10 +21,9 @@ export default class FootnoteAfterPunctuation extends RuleBuilder { - // 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) => { + // needed to ensure that no footnote text followed by ":" is matched + return text.replace(/(\[\^\w+\]) ?([,.;!:?])/gm, '$2$1'); }); } get exampleBuilders(): ExampleBuilder[] { @@ -38,6 +37,19 @@ export default class FootnoteAfterPunctuation extends RuleBuilder[] { diff --git a/src/utils/ignore-types.ts b/src/utils/ignore-types.ts index 0f431901..67c83c4b 100644 --- a/src/utils/ignore-types.ts +++ b/src/utils/ignore-types.ts @@ -27,6 +27,8 @@ export const IgnoreTypes: Record = { 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; From af1b4c69add3513fd06c396aad1d11289faf3f5d Mon Sep 17 00:00:00 2001 From: Peter Kaufman Date: Wed, 30 Nov 2022 21:37:06 -0500 Subject: [PATCH 2/5] updated the regex for tables to not include the a newline character after the table and then added a UT to make sure it works for paragraph blank lines which is where the issue was reported --- __tests__/paragraph-blank-lines.test.ts | 31 +++++++++++++++++++++++++ src/utils/regex.ts | 4 ++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/__tests__/paragraph-blank-lines.test.ts b/__tests__/paragraph-blank-lines.test.ts index b4644e9d..368b1f40 100644 --- a/__tests__/paragraph-blank-lines.test.ts +++ b/__tests__/paragraph-blank-lines.test.ts @@ -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()\` | | + + ### 表达式 + `, + }, ], }); diff --git a/src/utils/regex.ts b/src/utils/regex.ts index 22646f44..fecfa08f 100644 --- a/src/utils/regex.ts +++ b/src/utils/regex.ts @@ -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 @@ -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++; From bc6597ce1593ed15b6a9424cd75219b4ec963f17 Mon Sep 17 00:00:00 2001 From: Peter Kaufman Date: Fri, 2 Dec 2022 12:52:26 -0500 Subject: [PATCH 3/5] added a fix for removing consecutive lines when whitespace is present in them --- __tests__/consecutive-blank-lines.test.ts | 99 +++++++++++++++++++++++ src/rules/consecutive-blank-lines.ts | 2 +- 2 files changed, 100 insertions(+), 1 deletion(-) diff --git a/__tests__/consecutive-blank-lines.test.ts b/__tests__/consecutive-blank-lines.test.ts index 1f866051..3caa9bcd 100644 --- a/__tests__/consecutive-blank-lines.test.ts +++ b/__tests__/consecutive-blank-lines.test.ts @@ -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 + `, + }, ], }); diff --git a/src/rules/consecutive-blank-lines.ts b/src/rules/consecutive-blank-lines.ts index af8dd5bc..6d9778c5 100644 --- a/src/rules/consecutive-blank-lines.ts +++ b/src/rules/consecutive-blank-lines.ts @@ -22,7 +22,7 @@ export default class ConsecutiveBlankLines extends RuleBuilder { - return text.replace(/\n{2,}/g, '\n\n'); + return text.replace(/(\n([\t\v\f\r \u00a0\u2000-\u200b\u2028-\u2029\u3000]+)?){2,}/g, '\n\n'); }); } get exampleBuilders(): ExampleBuilder[] { From fa06137ef9759811c83aed78d17db92707b3a2f2 Mon Sep 17 00:00:00 2001 From: Peter Kaufman Date: Fri, 2 Dec 2022 13:00:38 -0500 Subject: [PATCH 4/5] removed now innacurate footnote info --- src/rules/footnote-after-punctuation.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/rules/footnote-after-punctuation.ts b/src/rules/footnote-after-punctuation.ts index 9cb3af8e..9e1d4555 100644 --- a/src/rules/footnote-after-punctuation.ts +++ b/src/rules/footnote-after-punctuation.ts @@ -22,7 +22,6 @@ export default class FootnoteAfterPunctuation extends RuleBuilder { - // needed to ensure that no footnote text followed by ":" is matched return text.replace(/(\[\^\w+\]) ?([,.;!:?])/gm, '$2$1'); }); } From ff4785be57eaee4363477aa0457d9b782e938148 Mon Sep 17 00:00:00 2001 From: Peter Kaufman Date: Fri, 2 Dec 2022 13:03:41 -0500 Subject: [PATCH 5/5] added comment with link to regex inspiration source --- src/rules/consecutive-blank-lines.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rules/consecutive-blank-lines.ts b/src/rules/consecutive-blank-lines.ts index 6d9778c5..c7d68fc7 100644 --- a/src/rules/consecutive-blank-lines.ts +++ b/src/rules/consecutive-blank-lines.ts @@ -22,6 +22,7 @@ export default class ConsecutiveBlankLines extends RuleBuilder { + // 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'); }); }