-
-
Notifications
You must be signed in to change notification settings - Fork 26
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
Ignore links to other branches #65
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,12 @@ export function config(ctx) { | |
|
||
if (info && info.type !== 'gist') { | ||
if (info.type in viewPaths) { | ||
urlConfig.prefix = '/' + info.path() + '/' + viewPaths[info.type] + '/' | ||
urlConfig.prefix = | ||
new URL(info.browse({treepath: viewPaths[info.type]})).pathname + '/' | ||
// GitHost#browse() omits the "blob" segment from branch-less URLs. | ||
if (!info.committish) { | ||
urlConfig.prefix += viewPaths[info.type] + '/' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again? |
||
} | ||
} | ||
|
||
if (info.type in headingPrefixes) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -248,11 +248,16 @@ function urlToPath(value, config, type) { | |
|
||
value = url.pathname.slice(config.urlConfig.prefix.length) | ||
|
||
// Drop the first path segment (the branch) if the prefix is branch-less (matches every branch). | ||
// Make an educated guess whether the prefix is branch-less: Currently every hosted-git-info browsetemplate/pathtemplate has <= 2 segments (`${user}/${project}`), plus e.g. "blob" and surrounding slashes -> 5. | ||
// | ||
// Things get interesting here: branches: `foo/bar/baz` could be `baz` on | ||
// the `foo/bar` branch, or, `baz` in the `bar` directory on the `foo` | ||
// branch. | ||
// Currently, we’re ignoring this and just not supporting branches. | ||
value = value.split(slash).slice(1).join(slash) | ||
if (config.urlConfig.prefix.split('/').length <= 5) { | ||
value = value.split(slash).slice(1).join(slash) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is way too magic? It doesn’t seem “educated”? Who says branches are 5+ characters? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I was wanting to get at wasn't the characters: '/DefinitelyTyped/DefinitelyTyped/blob/'.split('/')`
// -> [ '', 'DefinitelyTyped', 'DefinitelyTyped', 'blob', '' ] so |
||
|
||
return normalize( | ||
path.resolve(config.root, value + (type === 'image' ? '' : url.hash)), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import {remark} from 'remark' | ||
import test from 'tape' | ||
import {engine as engineCb} from 'unified-engine' | ||
import {promisify} from 'node:util' | ||
import {VFile} from 'vfile' | ||
import remarkValidateLinks from '../index.js' | ||
|
||
const engine = promisify(engineCb) | ||
|
||
// Should ignore links to other branches. | ||
const options = {repository: 'https://github.com/wooorm/test.git#main'} | ||
|
||
test('same branch, working link', async (t) => { | ||
t.deepEqual( | ||
await process( | ||
'[](https://github.com/wooorm/test/blob/main/examples/github.md#hello)' | ||
), | ||
[], | ||
'nothing to report' | ||
) | ||
}) | ||
|
||
test('other branch, working link', async (t) => { | ||
t.deepEqual( | ||
await process( | ||
'[](https://github.com/wooorm/test/blob/foo-bar/examples/github.md#hello)' | ||
), | ||
[], | ||
'nothing to ignore' | ||
) | ||
}) | ||
|
||
test('same branch, no such heading', async (t) => { | ||
t.deepEqual( | ||
await process( | ||
'[](https://github.com/wooorm/test/blob/main/examples/github.md#world)' | ||
), | ||
[ | ||
'input.md:1:1-1:70: Link to unknown heading in `examples/github.md`: `world`' | ||
], | ||
'should be reported' | ||
) | ||
}) | ||
|
||
test('other branch, no such heading', async (t) => { | ||
t.deepEqual( | ||
await process( | ||
'[](https://github.com/wooorm/test/blob/foo-bar/examples/github.md#world)' | ||
), | ||
[], | ||
'should be ignored' | ||
) | ||
}) | ||
|
||
test('same branch, no such file', async (t) => { | ||
t.deepEqual( | ||
await process( | ||
'[](https://github.com/wooorm/test/blob/main/examples/world.md#hello)' | ||
), | ||
[ | ||
'input.md:1:1-1:69: Link to unknown file: `examples/world.md`', | ||
'input.md:1:1-1:69: Link to unknown heading in `examples/world.md`: `hello`' | ||
], | ||
'should be reported' | ||
) | ||
}) | ||
|
||
test('other branch, no such file', async (t) => { | ||
t.deepEqual( | ||
await process( | ||
'[](https://github.com/wooorm/test/blob/foo-bar/examples/world.md#hello)' | ||
), | ||
[], | ||
'should be ignored' | ||
) | ||
}) | ||
|
||
/** | ||
* Run the plugin on a markdown test case and return a list of stringified messages. | ||
* unified-engine is needed to cross-reference links between files, by creating a FileSet. | ||
* | ||
* @param {string} value | ||
*/ | ||
async function process(value) { | ||
const file = new VFile({value, path: 'input.md'}) | ||
await engine({ | ||
processor: remark, | ||
files: [file], | ||
plugins: [[remarkValidateLinks, options]], | ||
silent: true | ||
}) | ||
return file.messages.map((message) => String(message)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks complex, compared to the previous code, what’s
browse
returning, what happensnew URL().pathname
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/npm/hosted-git-info/blob/702b7df467f2fd4bf46395134c62534bb966ca7b/git-host-info.js#L8
so hosted-git-info omits the "blob" segment from branch-less URLs.