Fix ranges and validation setting for MD own path + header links (#152270)

* Fix ranges and validation setting for MD own path + header links

Previously for a `file.md`, links to headers in that file that use paths, such as `[link](./file.md#some-header)` were validated using `markdown.experimental.validate.fragmentLinks.enabled`

This is confusing as that setting was only meant to be used for links such as`[link](#some-header`). It also resulted in the diagnostic having the incorrect range

This change instead makes these links be validated by `markdown.experimental.validate.fileLinks.markdownFragmentLinks`

* Fix compile
This commit is contained in:
Matt Bierner
2022-06-15 20:02:41 -07:00
committed by GitHub
parent 16c2a3ab3b
commit 4fc5d76213
2 changed files with 49 additions and 8 deletions

View File

@@ -347,4 +347,48 @@ suite('markdown: Diagnostics', () => {
new vscode.Range(5, 7, 5, 17),
]);
});
test('Should generate diagnostics for non-existent header using file link to own file', async () => {
const doc = new InMemoryDocument(workspacePath('sub', 'doc.md'), joinLines(
`[bad](doc.md#no-such)`,
`[bad](doc#no-such)`,
`[bad](/sub/doc.md#no-such)`,
`[bad](/sub/doc#no-such)`,
));
const diagnostics = await getComputedDiagnostics(doc, new InMemoryWorkspaceMarkdownDocuments([doc]));
assertDiagnosticsEqual(orderDiagnosticsByRange(diagnostics), [
new vscode.Range(0, 12, 0, 20),
new vscode.Range(1, 9, 1, 17),
new vscode.Range(2, 17, 2, 25),
new vscode.Range(3, 14, 3, 22),
]);
});
test('Own header link using file path link should be controlled by "validateMarkdownFileLinkFragments" instead of "validateFragmentLinks"', async () => {
const doc = new InMemoryDocument(workspacePath('sub', 'doc.md'), joinLines(
`[bad](doc.md#no-such)`,
`[bad](doc#no-such)`,
`[bad](/sub/doc.md#no-such)`,
`[bad](/sub/doc#no-such)`,
));
const contents = new InMemoryWorkspaceMarkdownDocuments([doc]);
const manager = createDiagnosticsManager(contents, new MemoryDiagnosticConfiguration({
validateFragmentLinks: DiagnosticLevel.ignore,
validateMarkdownFileLinkFragments: DiagnosticLevel.warning,
}));
const { diagnostics } = await manager.recomputeDiagnosticState(doc, noopToken);
assertDiagnosticsEqual(orderDiagnosticsByRange(diagnostics), [
new vscode.Range(0, 12, 0, 20),
new vscode.Range(1, 9, 1, 17),
new vscode.Range(2, 17, 2, 25),
new vscode.Range(3, 14, 3, 22),
]);
});
});
function orderDiagnosticsByRange(diagnostics: Iterable<vscode.Diagnostic>): readonly vscode.Diagnostic[] {
return Array.from(diagnostics).sort((a, b) => a.range.start.compareTo(b.range.start));
}