Skip to content

Commit

Permalink
feat(markdown): support user defined diagnostic options (#67)
Browse files Browse the repository at this point in the history
The diagnostics now depend on the configuration provided in the user's
editor settings.
  • Loading branch information
remcohaszing committed Dec 19, 2023
1 parent ac8281d commit 87ddb8d
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions packages/markdown/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { forEachEmbeddedFile, type FileChangeType, type FileType, type ServicePlugin, ServicePluginInstance } from '@volar/language-service';
import { Emitter } from 'vscode-jsonrpc';
import type { TextDocument } from 'vscode-languageserver-textdocument';
import type { ILogger, IMdLanguageService, IMdParser, IWorkspace } from 'vscode-markdown-languageservice';
import { DiagnosticLevel, LogLevel, createLanguageService, githubSlugifier } from 'vscode-markdown-languageservice';
import type { DiagnosticOptions, ILogger, IMdLanguageService, IMdParser, IWorkspace } from 'vscode-markdown-languageservice';
import { LogLevel, createLanguageService, githubSlugifier } from 'vscode-markdown-languageservice';
import { URI } from 'vscode-uri';
import MarkdownIt = require('markdown-it');

export interface Provide {
'markdown/languageService': () => IMdLanguageService;
}

export interface CreateOptions {
/**
* The section to use for configuring validation options.
*
* @example 'markdown.validate'
*/
configurationSection: string;
}

const md = new MarkdownIt();

function isMarkdown(document: TextDocument): boolean {
Expand All @@ -22,13 +31,13 @@ function assert(condition: unknown, message: string): asserts condition {
}
}

export function create(): ServicePlugin {
export function create(options: CreateOptions): ServicePlugin {
return {
create(context): ServicePluginInstance<Provide> {

let lastProjectVersion: string | undefined;

const { fs, onDidChangeWatchedFiles } = context.env;
const { fs, getConfiguration, onDidChangeWatchedFiles } = context.env;
assert(fs, 'context.env.fs must be defined');
assert(
onDidChangeWatchedFiles,
Expand Down Expand Up @@ -226,21 +235,16 @@ export function create(): ServicePlugin {
}
},

provideDiagnostics(document, token) {
async provideDiagnostics(document, token) {
if (prepare(document)) {
return ls.computeDiagnostics(
document,
{
ignoreLinks: [],
validateDuplicateLinkDefinitions: DiagnosticLevel.warning,
validateFileLinks: DiagnosticLevel.warning,
validateFragmentLinks: DiagnosticLevel.warning,
validateMarkdownFileLinkFragments: DiagnosticLevel.warning,
validateReferences: DiagnosticLevel.warning,
validateUnusedLinkDefinitions: DiagnosticLevel.warning
},
token
);
const configuration = await getConfiguration?.(options.configurationSection, document.uri);
if (configuration) {
return ls.computeDiagnostics(
document,
configuration as DiagnosticOptions,
token
);
}
}
},

Expand Down

0 comments on commit 87ddb8d

Please sign in to comment.