From dbe7578775a562074366242f298b9ee565f8ea02 Mon Sep 17 00:00:00 2001 From: Peter Kaufman Date: Fri, 27 Jan 2023 14:32:13 -0500 Subject: [PATCH 1/2] updated no bare urls to ignore anchor tags --- __tests__/no-bare-urls.test.ts | 9 +++++++++ src/rules/no-bare-urls.ts | 2 +- src/utils/ignore-types.ts | 3 ++- src/utils/regex.ts | 1 + 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/__tests__/no-bare-urls.test.ts b/__tests__/no-bare-urls.test.ts index 4c7cab01..40e9d053 100644 --- a/__tests__/no-bare-urls.test.ts +++ b/__tests__/no-bare-urls.test.ts @@ -47,5 +47,14 @@ ruleTest({ \`http --headers --follow --all https://google.com\` `, }, + {// https://github.com/platers/obsidian-linter/issues/588 + testName: 'Make sure that anchor tags are not affected by the rule', + before: dedent` + https://www.google.com + `, + after: dedent` + https://www.google.com + `, + }, ], }); diff --git a/src/rules/no-bare-urls.ts b/src/rules/no-bare-urls.ts index 52d7ccad..df3ac23a 100644 --- a/src/rules/no-bare-urls.ts +++ b/src/rules/no-bare-urls.ts @@ -25,7 +25,7 @@ export default class NoBareUrls extends RuleBuilder { return RuleType.CONTENT; } apply(text: string, options: NoBareUrlsOptions): string { - return ignoreListOfTypes([IgnoreTypes.code, IgnoreTypes.math, IgnoreTypes.yaml, IgnoreTypes.link, IgnoreTypes.wikiLink, IgnoreTypes.tag, IgnoreTypes.image, IgnoreTypes.inlineCode], text, (text) => { + return ignoreListOfTypes([IgnoreTypes.code, IgnoreTypes.math, IgnoreTypes.yaml, IgnoreTypes.link, IgnoreTypes.wikiLink, IgnoreTypes.tag, IgnoreTypes.image, IgnoreTypes.inlineCode, IgnoreTypes.anchorTag], text, (text) => { const URLMatches = text.match(urlRegex); if (!URLMatches) { diff --git a/src/utils/ignore-types.ts b/src/utils/ignore-types.ts index 718666ec..9cbb111d 100644 --- a/src/utils/ignore-types.ts +++ b/src/utils/ignore-types.ts @@ -1,4 +1,4 @@ -import {obsidianMultilineCommentRegex, tagWithLeadingWhitespaceRegex, wikiLinkRegex, yamlRegex, escapeDollarSigns, genericLinkRegex, tableRegex, urlRegex} from './regex'; +import {obsidianMultilineCommentRegex, tagWithLeadingWhitespaceRegex, wikiLinkRegex, yamlRegex, escapeDollarSigns, genericLinkRegex, tableRegex, urlRegex, anchorTagRegex} from './regex'; import {getPositions, MDAstTypes} from './mdast'; import type {Position} from 'unist'; import {replaceTextBetweenStartAndEndWithNewValue} from './strings'; @@ -29,6 +29,7 @@ export const IgnoreTypes: Record = { footnoteAtStartOfLine: {replaceAction: /^(\[\^\w+\]) ?([,.;!:?])/gm, placeholder: '{FOOTNOTE_AT_START_OF_LINE_PLACEHOLDER}'}, footnoteAfterATask: {replaceAction: /- \[.] (\[\^\w+\]) ?([,.;!:?])/gm, placeholder: '{FOOTNOTE_AFTER_A_TASK_PLACEHOLDER}'}, url: {replaceAction: urlRegex, placeholder: '{URL_PLACEHOLDER}'}, + anchorTag: {replaceAction: anchorTagRegex, placeholder: '{ANCHOR_PLACEHOLDER}'}, // custom functions link: {replaceAction: replaceMarkdownLinks, placeholder: '{REGULAR_LINK_PLACEHOLDER}'}, } as const; diff --git a/src/utils/regex.ts b/src/utils/regex.ts index 3ea3b75e..486cbdf7 100644 --- a/src/utils/regex.ts +++ b/src/utils/regex.ts @@ -22,6 +22,7 @@ export const lineStartingWithWhitespaceOrBlockquoteTemplate = `\\s*(>\\s*)*`; // 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]*?)*)+/gm; 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; +export const anchorTagRegex = /]+)>((?:.(?!\<\/a\>))*.)<\/a>/g; // https://stackoverflow.com/questions/38866071/javascript-replace-method-dollar-signs // Important to use this for any regex replacements where the replacement string From 117af1b8b5712ac76678ba67ecbb1cc854fa838a Mon Sep 17 00:00:00 2001 From: Peter Kaufman Date: Fri, 27 Jan 2023 14:42:38 -0500 Subject: [PATCH 2/2] removed unnecessary escaping of the regex --- src/utils/regex.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/regex.ts b/src/utils/regex.ts index 486cbdf7..0b3b84b9 100644 --- a/src/utils/regex.ts +++ b/src/utils/regex.ts @@ -22,7 +22,7 @@ export const lineStartingWithWhitespaceOrBlockquoteTemplate = `\\s*(>\\s*)*`; // 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]*?)*)+/gm; 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; -export const anchorTagRegex = /]+)>((?:.(?!\<\/a\>))*.)<\/a>/g; +export const anchorTagRegex = /]+)>((?:.(?!<\/a>))*.)<\/a>/g; // https://stackoverflow.com/questions/38866071/javascript-replace-method-dollar-signs // Important to use this for any regex replacements where the replacement string