From eb2074e7b3ce4e5af610eb825a29fa92d3c4a246 Mon Sep 17 00:00:00 2001 From: Loilo Date: Thu, 12 Dec 2019 02:07:38 +0100 Subject: [PATCH] Allow links to point to the top of the document --- lib/find/config.js | 13 ++++++++++++- lib/find/find.js | 7 ++++++- test/fixtures/github.md | 4 ++++ test/fixtures/gitlab.md | 4 ++++ test/index.js | 3 ++- 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/find/config.js b/lib/find/config.js index 5fae954..fddf5a5 100644 --- a/lib/find/config.js +++ b/lib/find/config.js @@ -6,6 +6,7 @@ module.exports = config var viewPaths = {github: 'blob', gitlab: 'blob', bitbucket: 'src'} var headingPrefixes = {github: '#', gitlab: '#', bitbucket: '#markdown-header-'} +var topAnchors = {github: '#readme', gitlab: '#readme'} var lineLinks = {github: true, gitlab: true} function config(ctx) { @@ -17,7 +18,13 @@ function config(ctx) { return } - urlConfig = {prefix: '', headingPrefix: '#', lines: false, hostname: null} + urlConfig = { + prefix: '', + headingPrefix: '#', + lines: false, + hostname: null, + topAnchor: null + } if (repo) { info = hostedGitInfo.fromUrl(repo) @@ -36,6 +43,10 @@ function config(ctx) { urlConfig.lines = lineLinks[info.type] } + if (info.type in topAnchors) { + urlConfig.topAnchor = topAnchors[info.type] + } + urlConfig.hostname = info.domain } diff --git a/lib/find/find.js b/lib/find/find.js index fc58891..c9ab68a 100644 --- a/lib/find/find.js +++ b/lib/find/find.js @@ -167,6 +167,7 @@ function normalize(url, config) { var numberSignIndex = url.indexOf(numberSign) var lines = config.lines var prefix = config.headingPrefix + var topAnchor = config.topAnchor var filePath var hash @@ -176,9 +177,13 @@ function normalize(url, config) { filePath = url.slice(0, numberSignIndex) hash = url.slice(numberSignIndex).toLowerCase() + // Ignore the hash if it references the top anchor of the environment + if (topAnchor && hash === topAnchor) { + hash = undefined + } // Ignore the hash if it references lines in a file or doesn’t start // with a heading prefix. - if ( + else if ( (lines && lineExpression.test(hash)) || hash.slice(0, prefix.length) !== prefix ) { diff --git a/test/fixtures/github.md b/test/fixtures/github.md index 7ef3b08..e3bb3ee 100644 --- a/test/fixtures/github.md +++ b/test/fixtures/github.md @@ -65,3 +65,7 @@ Valid: [b](http://example.com). Valid: [b](http://example.com/foo/bar/baz). Valid: [b](http://bitbucket.com/wooorm/test/blob/foo-bar/examples/world.md#hello). + +## Top Anchor + +This links to the start of the document [link](#readme). diff --git a/test/fixtures/gitlab.md b/test/fixtures/gitlab.md index b4bcb46..08df49c 100644 --- a/test/fixtures/gitlab.md +++ b/test/fixtures/gitlab.md @@ -61,3 +61,7 @@ Valid: [b](http://example.com). Valid: [b](http://example.com/foo/bar/baz). Valid: [b](http://bitbucket.com/wooorm/test/blob/foo-bar/examples/world.md#hello). + +## Top Anchor + +This links to the start of the document [link](#readme). diff --git a/test/index.js b/test/index.js index 2136a6b..c8da2d6 100644 --- a/test/index.js +++ b/test/index.js @@ -663,8 +663,9 @@ test('remark-validate-links', function(t) { ' 49:10-49:40 warning Link to unknown heading in `examples/world.md`: `hello` missing-heading-in-file remark-validate-links', ' 51:10-51:38 warning Link to unknown file: `examples/world.md` missing-file remark-validate-links', ' 51:10-51:38 warning Link to unknown heading in `examples/world.md`: `hello` missing-heading-in-file remark-validate-links', + ' 71:41-71:56 warning Link to unknown heading: `readme` missing-heading remark-validate-links', '', - '⚠ 14 warnings' + '⚠ 15 warnings' ].join('\n'), 'should report' )