git.iko.soy/typeable/remark-validate-links
on main help sign in
Add support for anchors to top of doc
46af9ebFlorian Reuschel
lib/find/config.js+12−1
@@ -6,6 +6,7 @@ module.exports = config
6
6
7var viewPaths = {github: 'blob', gitlab: 'blob', bitbucket: 'src'}
7var viewPaths = {github: 'blob', gitlab: 'blob', bitbucket: 'src'}
8var headingPrefixes = {github: '#', gitlab: '#', bitbucket: '#markdown-header-'}
8var headingPrefixes = {github: '#', gitlab: '#', bitbucket: '#markdown-header-'}
9+var topAnchors = {github: '#readme', gitlab: '#readme'}
9var lineLinks = {github: true, gitlab: true}
10var lineLinks = {github: true, gitlab: true}
10
11
11function config(ctx) {
12function config(ctx) {
@@ -17,7 +18,13 @@ function config(ctx) {
17 return
18 return
18 }
19 }
19
20
21+ urlConfig = {
22+ prefix: '',
20− urlConfig = {prefix: '', headingPrefix: '#', lines: false, hostname: null}
23+ headingPrefix: '#',
24+ lines: false,
25+ hostname: null,
26+ topAnchor: null
27+ }
21
28
22 if (repo) {
29 if (repo) {
23 info = hostedGitInfo.fromUrl(repo)
30 info = hostedGitInfo.fromUrl(repo)
@@ -36,6 +43,10 @@ function config(ctx) {
36 urlConfig.lines = lineLinks[info.type]
43 urlConfig.lines = lineLinks[info.type]
37 }
44 }
38
45
46+ if (info.type in topAnchors) {
47+ urlConfig.topAnchor = topAnchors[info.type]
48+ }
49+
39 urlConfig.hostname = info.domain
50 urlConfig.hostname = info.domain
40 }
51 }
41
52
lib/find/find.js+6−1
@@ -167,6 +167,7 @@ function normalize(url, config) {
167 var numberSignIndex = url.indexOf(numberSign)
167 var numberSignIndex = url.indexOf(numberSign)
168 var lines = config.lines
168 var lines = config.lines
169 var prefix = config.headingPrefix
169 var prefix = config.headingPrefix
170+ var topAnchor = config.topAnchor
170 var filePath
171 var filePath
171 var hash
172 var hash
172
173
@@ -176,9 +177,13 @@ function normalize(url, config) {
176 filePath = url.slice(0, numberSignIndex)
177 filePath = url.slice(0, numberSignIndex)
177 hash = url.slice(numberSignIndex).toLowerCase()
178 hash = url.slice(numberSignIndex).toLowerCase()
178
179
180+ // Ignore the hash if it references the top anchor of the environment
181+ if (topAnchor && hash === topAnchor) {
182+ hash = undefined
183+ }
179 // Ignore the hash if it references lines in a file or doesn’t start
184 // Ignore the hash if it references lines in a file or doesn’t start
180 // with a heading prefix.
185 // with a heading prefix.
181− if (
186+ else if (
182 (lines && lineExpression.test(hash)) ||
187 (lines && lineExpression.test(hash)) ||
183 hash.slice(0, prefix.length) !== prefix
188 hash.slice(0, prefix.length) !== prefix
184 ) {
189 ) {
test/fixtures/github.md+4
@@ -65,3 +65,7 @@ Valid: [b](http://example.com).
65Valid: [b](http://example.com/foo/bar/baz).
65Valid: [b](http://example.com/foo/bar/baz).
66
66
67Valid: [b](http://bitbucket.com/wooorm/test/blob/foo-bar/examples/world.md#hello).
67Valid: [b](http://bitbucket.com/wooorm/test/blob/foo-bar/examples/world.md#hello).
68+
69+## Top Anchor
70+
71+This links to the start of the document [link](#readme).
test/fixtures/gitlab.md+4
@@ -61,3 +61,7 @@ Valid: [b](http://example.com).
61Valid: [b](http://example.com/foo/bar/baz).
61Valid: [b](http://example.com/foo/bar/baz).
62
62
63Valid: [b](http://bitbucket.com/wooorm/test/blob/foo-bar/examples/world.md#hello).
63Valid: [b](http://bitbucket.com/wooorm/test/blob/foo-bar/examples/world.md#hello).
64+
65+## Top Anchor
66+
67+This links to the start of the document [link](#readme).
test/index.js+2−1
@@ -663,8 +663,9 @@ test('remark-validate-links', function(t) {
663 ' 49:10-49:40 warning Link to unknown heading in `examples/world.md`: `hello` missing-heading-in-file remark-validate-links',
663 ' 49:10-49:40 warning Link to unknown heading in `examples/world.md`: `hello` missing-heading-in-file remark-validate-links',
664 ' 51:10-51:38 warning Link to unknown file: `examples/world.md` missing-file remark-validate-links',
664 ' 51:10-51:38 warning Link to unknown file: `examples/world.md` missing-file remark-validate-links',
665 ' 51:10-51:38 warning Link to unknown heading in `examples/world.md`: `hello` missing-heading-in-file remark-validate-links',
665 ' 51:10-51:38 warning Link to unknown heading in `examples/world.md`: `hello` missing-heading-in-file remark-validate-links',
666+ ' 71:41-71:56 warning Link to unknown heading: `readme` missing-heading remark-validate-links',
666 '',
667 '',
667− '⚠ 14 warnings'
668+ '⚠ 15 warnings'
668 ].join('\n'),
669 ].join('\n'),
669 'should report'
670 'should report'
670 )
671 )