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

Add No Bare URLs Rule #222

Merged
merged 2 commits into from
Jun 10, 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
65 changes: 60 additions & 5 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -360,7 +360,7 @@ After:
## THIS IS A HEADING 2
### A hEaDiNg 3
```
Example: With `First Letter=true`
Example: With `First letter=true`

Before:

Expand All @@ -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:

Expand Down Expand Up @@ -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
<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>
```
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
[<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"
```

## Spacing
### Trailing spaces

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
76 changes: 76 additions & 0 deletions src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
<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>
`,
),
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`
[<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"
`,
),
],
),

// YAML rules

Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"1.2.0": "0.9.7"
"1.3.2": "0.9.7"
}