Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow links to point to the top of the document #48

Merged
merged 1 commit into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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