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

No Bare URLs Add Smart Typography as Acceptable Wrapping Characters and Ignore Inline Code #471

Merged
merged 2 commits into from
Nov 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
20 changes: 20 additions & 0 deletions __tests__/no-bare-urls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,25 @@ ruleTest({
<https://google.com#hashtag>
`,
},
{// 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\`
`,
},
],
});
16 changes: 9 additions & 7 deletions src/rules/no-bare-urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {replaceTextBetweenStartAndEndWithNewValue} from '../utils/strings';
class NoBareUrlsOptions implements Options {
}

const specialCharsToNotEscapeContentsWithin = `'"‘’“”\`[]`;

@RuleBuilder.register
export default class NoBareUrls extends RuleBuilder<NoBareUrlsOptions> {
get OptionsClass(): new () => NoBareUrlsOptions {
Expand All @@ -22,8 +24,8 @@ export default class NoBareUrls extends RuleBuilder<NoBareUrlsOptions> {
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;
Expand All @@ -39,8 +41,8 @@ export default class NoBareUrls extends RuleBuilder<NoBareUrlsOptions> {

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;
}
Expand Down Expand Up @@ -97,16 +99,16 @@ export default class NoBareUrls extends RuleBuilder<NoBareUrlsOptions> {
`,
}),
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`
[<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"
`,
Expand Down