From c0cc9d359ef26c4b38d8d4d96168262cc5ccc13d Mon Sep 17 00:00:00 2001 From: Jennifer Shehane Date: Fri, 16 Jun 2017 12:43:09 -0400 Subject: [PATCH] docs: url tag now throws if given undefined href --- docs/lib/url_generator.js | 14 +++++++++++++- docs/scripts/tags.js | 2 +- docs/test/unit/url_generator_spec.coffee | 21 ++++++++++++++++++--- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/docs/lib/url_generator.js b/docs/lib/url_generator.js index 1636806a07..640a8cfb7d 100644 --- a/docs/lib/url_generator.js +++ b/docs/lib/url_generator.js @@ -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] diff --git a/docs/scripts/tags.js b/docs/scripts/tags.js index 614743794f..a116eaab14 100644 --- a/docs/scripts/tags.js +++ b/docs/scripts/tags.js @@ -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 diff --git a/docs/test/unit/url_generator_spec.coffee b/docs/test/unit/url_generator_spec.coffee index cad2ee6fd4..52730b7831 100644 --- a/docs/test/unit/url_generator_spec.coffee +++ b/docs/test/unit/url_generator_spec.coffee @@ -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("
notes
") + + 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) ->