From 8403b17d09f7c96f157dd03ad5ad3d366a7cfab8 Mon Sep 17 00:00:00 2001 From: Peter Kaufman Date: Wed, 2 Nov 2022 18:29:50 -0400 Subject: [PATCH] add smart typography to characters that are acceptable wrapping characters for bare urls and went ahead and ignored inline code --- __tests__/no-bare-urls.test.ts | 20 ++++++++++++++++++++ src/rules/no-bare-urls.ts | 16 +++++++++------- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/__tests__/no-bare-urls.test.ts b/__tests__/no-bare-urls.test.ts index 52701801..4c7cab01 100644 --- a/__tests__/no-bare-urls.test.ts +++ b/__tests__/no-bare-urls.test.ts @@ -27,5 +27,25 @@ ruleTest({ `, }, + {// accounts for https://github.com/platers/obsidian-linter/issues/469 + testName: 'Urls that are surrounded by smart quotes should be left alone', + before: dedent` + “https://google.com” + ‘https://google.com’ + `, + after: dedent` + “https://google.com” + ‘https://google.com’ + `, + }, + {// accounts for https://github.com/platers/obsidian-linter/issues/469 + testName: 'Urls that are in inline code should be left alone', + before: dedent` + \`http --headers --follow --all https://google.com\` + `, + after: dedent` + \`http --headers --follow --all https://google.com\` + `, + }, ], }); diff --git a/src/rules/no-bare-urls.ts b/src/rules/no-bare-urls.ts index 798a4f78..ca625d41 100644 --- a/src/rules/no-bare-urls.ts +++ b/src/rules/no-bare-urls.ts @@ -7,6 +7,8 @@ import {replaceTextBetweenStartAndEndWithNewValue} from '../utils/strings'; class NoBareUrlsOptions implements Options { } +const specialCharsToNotEscapeContentsWithin = `'"‘’“”\`[]`; + @RuleBuilder.register export default class NoBareUrls extends RuleBuilder { get OptionsClass(): new () => NoBareUrlsOptions { @@ -22,8 +24,8 @@ export default class NoBareUrls extends RuleBuilder { return RuleType.CONTENT; } apply(text: string, options: NoBareUrlsOptions): string { - return ignoreListOfTypes([IgnoreTypes.code, IgnoreTypes.yaml, IgnoreTypes.link, IgnoreTypes.wikiLink, IgnoreTypes.tag, IgnoreTypes.image], text, (text) => { - const URLMatches = text.match(/(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); + return ignoreListOfTypes([IgnoreTypes.code, IgnoreTypes.yaml, IgnoreTypes.link, IgnoreTypes.wikiLink, IgnoreTypes.tag, IgnoreTypes.image, IgnoreTypes.inlineCode], text, (text) => { + const URLMatches = text.match(/(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); if (!URLMatches) { return text; @@ -39,8 +41,8 @@ export default class NoBareUrls extends RuleBuilder { const previousChar = urlStart === 0 ? undefined : text.charAt(urlStart - 1); const nextChar = urlEnd >= text.length ? undefined : text.charAt(urlEnd); - if (previousChar != undefined && (previousChar === '`' || previousChar === '"' || previousChar === '\'' || previousChar === '[') && - nextChar != undefined && (nextChar === '`' || nextChar === '"' || nextChar === '\'' || nextChar === ']')) { + if (previousChar != undefined && specialCharsToNotEscapeContentsWithin.includes(previousChar) && + nextChar != undefined && specialCharsToNotEscapeContentsWithin.includes(nextChar)) { startSearch = urlStart + urlMatch.length; continue; } @@ -97,16 +99,16 @@ export default class NoBareUrls extends RuleBuilder { `, }), new ExampleBuilder({ - description: 'Angle brackets are added if the url is not the only text in the single quotes(\'), double quotes("), or backticks(`)', + description: 'Angle brackets are added if the url is not the only text in the single quotes(\') or double quotes(")', before: dedent` [https://github.com some text here] - backticks around a url should stay the same, but only if the only contents of the backticks: \`https://github.com some text here\` + backticks around a url should stay the same: \`https://github.com some text here\` single quotes around a url should stay the same, but only if the contents of the single quotes is the url: 'https://github.com some text here' double quotes around a url should stay the same, but only if the contents of the double quotes is the url: "https://github.com some text here" `, after: dedent` [ some text here] - backticks around a url should stay the same, but only if the only contents of the backticks: \` some text here\` + backticks around a url should stay the same: \`https://github.com some text here\` single quotes around a url should stay the same, but only if the contents of the single quotes is the url: ' some text here' double quotes around a url should stay the same, but only if the contents of the double quotes is the url: " some text here" `,