Skip to content

Commit

Permalink
Add support for anchors to top of doc
Browse files Browse the repository at this point in the history
Closes GH-48.

Reviewed-by: Titus Wormer <[email protected]>
  • Loading branch information
loilo authored and wooorm committed Dec 12, 2019
1 parent 13a54a5 commit 46af9eb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
13 changes: 12 additions & 1 deletion lib/find/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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
}

Expand Down
7 changes: 6 additions & 1 deletion lib/find/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
) {
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/github.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
4 changes: 4 additions & 0 deletions test/fixtures/gitlab.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
3 changes: 2 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
Expand Down

0 comments on commit 46af9eb

Please sign in to comment.