Skip to content

Commit

Permalink
Implement useLinkRef config and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kiranparajuli589 committed Nov 13, 2023
1 parent ba56ce9 commit 7b3860b
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/lexer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class Lexer {

run() {
this.#skipFrontMatter()
this.#checkForLinkRefs()
if (this.#config.useLinkRefs) {
this.#checkForLinkRefs()
}

for (this.#cursor = 0; this.#cursor < this.#lines.length; this.#cursor++) {
this.#runPrep()
Expand Down
2 changes: 1 addition & 1 deletion lib/regex/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const REGEX = {
},
HTML: /^\s*<(?<tag>\w+)\s+(?<attrs>\b[^>]*)>(?<content>.*)<\/(?<endTag>\w+)>/s,
LINK_REF: {
DECLARATION: /\s*\[(?<text>.+)]:\s+(?<href>\S+(?:\s'(?<title>.+?)')?)/g,
DECLARATION: /\s*\[(?<text>.+)]:\s+(?<href>\S+)(?:\s['"](?<title>.+?)['"])?/g,
WITH_TEXT: /^(?<!!)\[(?<text>.+?)](?!\(.+\))(?<!!)\[(?<ref>.+?)](?!\(.+\))/,
WITHOUT_TEXT: /^(?<!!)\[(?<ref>.+?)](?!\(.+\))/
},
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/lexer/__snapshots__/codeblock.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,22 @@ const a = 1
]
`;
exports[`codeblock should remove the link refs inside codeblock but do not change the usage 1`] = `
Array [
Object {
"indent": 0,
"language": "markdown",
"raw": "\`\`\` markdown
[foo][]
\`\`\`",
"type": "code-block",
"value": "
[foo][]",
},
]
`;
exports[`codeblock should set incomplete codeblock as a code block 1`] = `
Array [
Object {
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/lexer/codeblock.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,15 @@ describe("codeblock", () => {
const tokens = lexer.run()
expect(tokens).toMatchSnapshot()
})
it("should remove the link refs inside codeblock but do not change the usage", () => {
const lines = `\`\`\` markdown
[foo]: /url1
[foo]: /url2
[foo][]
\`\`\``
const lexer = new Lexer(lines.split("\n"))
const tokens = lexer.run()
expect(tokens).toMatchSnapshot()
})
})
23 changes: 23 additions & 0 deletions tests/unit/parser/__snapshots__/linkRefs.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Link Reference should not use the link references for link construction when disabled from config 1`] = `
"<p><a href=\\"https://www.somewebsite.com\\">I&#39;m an inline-style link</a></p>
<p><a href=\\"https://www.somewebsite.com\\" title=\\"somewebsite's Homepage\\">I&#39;m an inline-style link with title</a></p>
<p>[I&#39;m a reference-style link][Arbitrary case-insensitive reference text]</p>
<p><a href=\\"../blob/master/LICENSE\\">I&#39;m a relative reference to a repository file</a></p>
<p><a href=\\"http://somewebsite.org\\" title=\\"somewebsite\\">You can use numbers for reference-style link definitions</a></p>
<p>Or leave it empty and use the <a href=\\"http://www.somewebsite.com\\">link text itself</a></p>
<p>Some text to show that the reference links can follow later.</p>
"
`;
exports[`Link Reference should use the link references for link construction 1`] = `
"<p><a href=\\"https://www.somewebsite.com\\">I&#39;m an inline-style link</a></p>
<p><a href=\\"https://www.somewebsite.com\\" title=\\"somewebsite's Homepage\\">I&#39;m an inline-style link with title</a></p>
<p>[I&#39;m a reference-style link][Arbitrary case-insensitive reference text]</p>
<p><a href=\\"../blob/master/LICENSE\\">I&#39;m a relative reference to a repository file</a></p>
<p><a href=\\"http://somewebsite.org\\" title=\\"somewebsite\\">You can use numbers for reference-style link definitions</a></p>
<p>Or leave it empty and use the <a href=\\"http://www.somewebsite.com\\">link text itself</a></p>
<p>Some text to show that the reference links can follow later.</p>
"
`;
35 changes: 35 additions & 0 deletions tests/unit/parser/linkRefs.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import HtmlMark from "../../../lib/index.js"


describe("Link Reference", () => {
const htmlMarkWithLinkRefsOn = new HtmlMark()
const htmlMarkWithLinkRefsOff = new HtmlMark({ useLinkRefs: false })

const lines = `[I'm an inline-style link](https://www.somewebsite.com)
[I'm an inline-style link with title](https://www.somewebsite.com "somewebsite's Homepage")
[I'm a reference-style link][Arbitrary case-insensitive reference text]
[I'm a relative reference to a repository file](../blob/master/LICENSE)
[You can use numbers for reference-style link definitions][1]
Or leave it empty and use the [link text itself]
Some text to show that the reference links can follow later.
[arbitrary case-insensitive reference text]: https://www.somewebsite.org
[1]: http://somewebsite.org "somewebsite's Homepage"
[link text itself]: http://www.somewebsite.com`

it("should use the link references for link construction", () => {
const html = htmlMarkWithLinkRefsOn.parse(lines)
expect(html).toMatchSnapshot()
})

it("should not use the link references for link construction when disabled from config", () => {
const html = htmlMarkWithLinkRefsOff.parse(lines)
expect(html).toMatchSnapshot()
})
})

0 comments on commit 7b3860b

Please sign in to comment.