diff --git a/README.md b/README.md index 3f57d39c..23122136 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ Documentation for all rules can be found in the [rules docs](https://github.com/ - [proper-ellipsis](https://github.com/platers/obsidian-linter/blob/master/docs/rules.md#proper-ellipsis) - [emphasis-style](https://github.com/platers/obsidian-linter/blob/master/docs/rules.md#emphasis-style) - [strong-style](https://github.com/platers/obsidian-linter/blob/master/docs/rules.md#strong-style) +- [no-bare-urls](https://github.com/platers/obsidian-linter/blob/master/docs/rules.md#no-bare-urls) ### Spacing rules diff --git a/docs/rules.md b/docs/rules.md index 0c476206..4647223a 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -316,9 +316,9 @@ Headings should be formatted with capitalization Options: - Style: The style of capitalization to use - Default: `Title Case` - - `Title Case`: Capitalize using title case rules - - `All Caps`: Capitalize the first letter of each word - - `First Letter`: Only capitalize the first letter + - `Title Case`: Capitalize Using Title Case Rules + - `ALL CAPS`: CAPITALIZE THE WHOLE TITLE + - `First letter`: Only capitalize the first letter - Ignore Cased Words: Only apply title case style to words that are all lowercase - Default: `true` - Ignore Words: A comma separated list of words to ignore when capitalizing @@ -360,7 +360,7 @@ After: ## THIS IS A HEADING 2 ### A hEaDiNg 3 ``` -Example: With `First Letter=true` +Example: With `First letter=true` Before: @@ -375,7 +375,7 @@ After: # This is a heading 1 ## This is a heading 2 ``` -Example: With `All Caps=true` +Example: With `ALL CAPS=true` Before: @@ -976,6 +976,61 @@ This is ___nested emphasis_ and ending bold__ __Test bold__ ``` +### No Bare URLs + +Alias: `no-bare-urls` + +Encloses bare URLs with angle brackets except when enclosed in back ticks, square braces, or single or double quotes. + + + +Example: Make sure that links are inside of angle brackets when not in single quotes('), double quotes("), or backticks(`) + +Before: + +```markdown +https://github.com +braces around url should stay the same: [https://github.com] +backticks around url should stay the same: `https://github.com` +Links mid-sentence should be updated like https://google.com will be. +'https://github.com' +"https://github.com" +links should stay the same: [](https://github.com) +https://gitlab.com +``` + +After: + +```markdown + +braces around url should stay the same: [https://github.com] +backticks around url should stay the same: `https://github.com` +Links mid-sentence should be updated like will be. +'https://github.com' +"https://github.com" +links should stay the same: [](https://github.com) + +``` +Example: Angle brackets are added if the url is not the only text in the single quotes('), double quotes("), or backticks(`) + +Before: + +```markdown +[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` +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: + +```markdown +[ some text here] +backticks around a url should stay the same, but only if the only contents of the backticks: ` 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" +``` + ## Spacing ### Trailing spaces diff --git a/manifest.json b/manifest.json index 292d6825..70121090 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-linter", "name": "Linter", - "version": "1.3.1", + "version": "1.3.2", "minAppVersion": "0.9.7", "description": "Enforces consistent markdown styling.", "author": "Victor Tao", diff --git a/src/main.ts b/src/main.ts index 901f6379..e9992eac 100644 --- a/src/main.ts +++ b/src/main.ts @@ -193,7 +193,7 @@ export default class LinterPlugin extends Plugin { const newText = this.lintText(oldText, file); await this.app.vault.modify(file, newText); } catch (error) { - new Notice('An error occured during linting. See console for details'); + new Notice('An error occurred during linting. See console for details'); console.log(`Linting error in file: ${file.path}`); console.error(error); } diff --git a/src/rules.ts b/src/rules.ts index 6176cce3..a7add76d 100644 --- a/src/rules.ts +++ b/src/rules.ts @@ -1103,6 +1103,82 @@ export const rules: Rule[] = [ ), ], ), + new Rule( + 'No Bare URLs', + 'Encloses bare URLs with angle brackets except when enclosed in back ticks, square braces, or single or double quotes.', + RuleType.CONTENT, + (text: string) => { + return ignoreCodeBlocksYAMLAndLinks(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; + } + + // make sure you do not match on the same thing more than once by keeping track of the last position you checked up to + let startSearch = 0; + const numMatches = URLMatches.length; + for (let i = 0; i < numMatches; i++) { + const urlMatch = URLMatches[i]; + const urlStart = text.indexOf(urlMatch, startSearch); + const urlEnd = urlStart + urlMatch.length; + + 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 === ']')) { + startSearch = urlStart + urlMatch.length; + continue; + } + + text = text.substring(0, urlStart) + '<' + urlMatch + '>' + text.substring(urlStart + urlMatch.length); + startSearch = urlStart + urlMatch.length + 2; + } + + return text; + }); + }, + [ + new Example( + 'Make sure that links are inside of angle brackets when not in single quotes(\'), double quotes("), or backticks(`)', + dedent` + https://github.com + braces around url should stay the same: [https://github.com] + backticks around url should stay the same: \`https://github.com\` + Links mid-sentence should be updated like https://google.com will be. + 'https://github.com' + "https://github.com" + links should stay the same: [](https://github.com) + https://gitlab.com + `, + dedent` + + braces around url should stay the same: [https://github.com] + backticks around url should stay the same: \`https://github.com\` + Links mid-sentence should be updated like will be. + 'https://github.com' + "https://github.com" + links should stay the same: [](https://github.com) + + `, + ), + new Example( + 'Angle brackets are added if the url is not the only text in the single quotes(\'), double quotes("), or backticks(`)', + 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\` + 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" + `, + dedent` + [ some text here] + backticks around a url should stay the same, but only if the only contents of the backticks: \` 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" + `, + ), + ], + ), // YAML rules diff --git a/versions.json b/versions.json index 5b33d01c..fbf8149f 100644 --- a/versions.json +++ b/versions.json @@ -1,3 +1,3 @@ { - "1.2.0": "0.9.7" + "1.3.2": "0.9.7" }