docs: url tag now throws if given undefined href

This commit is contained in:
Jennifer Shehane
2017-06-16 12:43:09 -04:00
parent 824537b1a7
commit c0cc9d359e
3 changed files with 32 additions and 5 deletions

View File

@@ -168,7 +168,19 @@ function validateLocalFile (sidebar, href, source, render) {
})
}
function validateAndGetUrl (sidebar, href, source, render) {
function validateAndGetUrl (sidebar, href, source, text, render) {
if (!href) {
// if we dont have a hash
return Promise.reject(
new Error(`A url tag was not passed an href argument.
> The source file was: ${source}
> url tag's text was: ${text}
`)
)
}
// do we already have a cache for this href?
const cachedValue = cache[href]

View File

@@ -142,7 +142,7 @@ hexo.extend.tag.register('url', function (args) {
return hexo.render.render({ text, engine: 'markdown' })
}
return urlGenerator.validateAndGetUrl(sidebar, attrs.href, this.full_source, onRender)
return urlGenerator.validateAndGetUrl(sidebar, attrs.href, this.full_source, props.text, onRender)
.then((href) => {
attrs.href = href

View File

@@ -95,6 +95,21 @@ describe "lib/url_generator", ->
expect(err.message).to.include("Could not find a valid doc file in the sidebar.yml for: foo")
context ".validateAndGetUrl", ->
it "fails when given undefined href", ->
render = (str) ->
return Promise.resolve("<html><div id='notes'>notes</div></html>")
urlGenerator.validateAndGetUrl(data, undefined, 'foo', 'content', render )
.then ->
throw new Error("should have caught error")
.catch (err) ->
[
"A url tag was not passed an href argument."
"The source file was: foo"
"url tag's text was: content",
].forEach (msg) ->
expect(err.message).to.include(msg)
it "fails when external returns non 2xx", ->
nock("https://www.google.com")
.get("/")
@@ -116,11 +131,11 @@ describe "lib/url_generator", ->
@sandbox.stub(fs, "readFile").returns(Promise.resolve(markdown))
urlGenerator.validateAndGetUrl(data, "and#notes", "", render)
urlGenerator.validateAndGetUrl(data, "and#notes", "", "", render)
.then (pathToFile) ->
expect(pathToFile).to.eq("/api/commands/and.html#notes")
urlGenerator.validateAndGetUrl(data, "and#notes", "", render)
urlGenerator.validateAndGetUrl(data, "and#notes", "", "", render)
.then (pathToFile) ->
expect(pathToFile).to.eq("/api/commands/and.html#notes")
@@ -164,7 +179,7 @@ describe "lib/url_generator", ->
@sandbox.stub(fs, "readFile").returns(Promise.resolve(""))
urlGenerator.validateAndGetUrl(data, "and#foo", "guides/core-concepts/bar.md", render)
urlGenerator.validateAndGetUrl(data, "and#foo", "guides/core-concepts/bar.md", "content", render)
.then ->
throw new Error("should have caught error")
.catch (err) ->