Skip to content

Commit

Permalink
resolves #99 improve autolink feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie committed Jan 13, 2024
1 parent a34a34e commit 64b4979
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/features/documentLinkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { AsciidocIncludeItemsLoader } from '../asciidocLoader'
* Reference: https://gist.github.com/dperini/729294
*/
// eslint-disable-next-line max-len
const urlRx = /(?:(?:https?|ftp|irc):)?\/\/(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4])|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+[a-z\u00a1-\uffff]{2,}\.?)(?::\d{2,5})?(?:[/?#][^[]*)?/ig
const urlRx = /(?<=|link|<|[>()[\];"'])\\?(?:https?|file|ftp|irc):\/\/[^\s[\]]+/gm
const inlineAnchorRx = /^\[\[(?<id>[^,]+)(?:,[^\]]+)*]]$/m
const xrefRx = /xref:(?<target>[^#|^[]+)(?<fragment>#[^[]+)?\[[^\]]*]/ig
const localize = nls.loadMessageBundle()
Expand Down Expand Up @@ -68,7 +68,7 @@ export default class LinkProvider implements vscode.DocumentLinkProvider {
if (urlsFound) {
for (const urlFound of urlsFound) {
const index = urlFound.index
const url = urlFound[0]
const url = urlFound[0].replace(/[,.;?!:)>]+$/, '')
const documentLink = new vscode.DocumentLink(
new vscode.Range(
new vscode.Position(lineNumber, index),
Expand Down
36 changes: 36 additions & 0 deletions src/test/documentLinkProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,40 @@ See xref:test.adoc#second-section[]
}))
assertRangeEqual(link.range, new vscode.Range(6, 9, 6, 33))
})

test('Should detect inline URL', async () => {
const links = await getLinksForFile(`= Title
You can refer to a URL such as https://github.com/asciidoctor/asciidoctor-vscode/, and continue the sentence or the paragraph.
`)
assert.strictEqual(links.length, 1)
const [link] = links
assert.deepStrictEqual(link.target.toString(), 'https://github.com/asciidoctor/asciidoctor-vscode/')
assertRangeEqual(link.range, new vscode.Range(2, 31, 2, 81))
})

test('Should detect inline URL within square brackets', async () => {
const links = await getLinksForFile(`= Title
Filters are created as RPN filters (Reverse Polish notation [https://wikipedia.org/wiki/Reverse_Polish_notation]) with the following syntax...
`)
assert.strictEqual(links.length, 1)
const [link] = links
assert.deepStrictEqual(link.target.toString(), 'https://wikipedia.org/wiki/Reverse_Polish_notation')
assertRangeEqual(link.range, new vscode.Range(2, 61, 2, 111))
})

test('Should detect inline URL within angle brackets', async () => {
const links = await getLinksForFile(`= Title
Asciidoctor.js is published as a npm package at <https://www.npmjs.com/package/@asciidoctor/core>.
`)
assert.strictEqual(links.length, 1)
const [link] = links
assert.deepStrictEqual(link.target.toString(true), 'https://www.npmjs.com/package/@asciidoctor/core')
assertRangeEqual(link.range, new vscode.Range(2, 49, 2, 96))
})
})

0 comments on commit 64b4979

Please sign in to comment.