Skip to content

Commit

Permalink
Merge pull request #222 from pjkaufman/master
Browse files Browse the repository at this point in the history
Add No Bare URLs Rule
  • Loading branch information
pjkaufman authored Jun 10, 2022
2 parents ed267a4 + e383863 commit 38d6426
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 8 deletions.
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"
}

0 comments on commit 38d6426

Please sign in to comment.