diff --git a/lib/index.js b/lib/index.js index b27f5ee..3fd3442 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,10 +3,15 @@ * @typedef {import('unist').Position} UnistPosition * @typedef {import('vfile-message').VFileMessage} VFileMessage * @typedef {import('vscode-languageserver').Connection} Connection - * @typedef {Pick< + * @typedef {Partial} Options + * | 'ignoreName' + * | 'packageField' + * | 'pluginPrefix' + * | 'plugins' + * | 'processor' + * | 'rcName' + * >>} Options */ import {PassThrough} from 'node:stream' @@ -136,7 +141,14 @@ function lspDocumentToVfile(document) { export function configureUnifiedLanguageServer( connection, documents, - {ignoreName, packageField, pluginPrefix, plugins, rcName} + { + ignoreName, + packageField, + pluginPrefix, + plugins, + processor = unified(), + rcName + } ) { /** * Process various LSP text documents using unified and send back the @@ -156,7 +168,7 @@ export function configureUnifiedLanguageServer( packageField, pluginPrefix, plugins, - processor: unified(), + processor, quiet: false, rcName, silentlyIgnore: true, diff --git a/readme.md b/readme.md index 34d76f4..913c634 100644 --- a/readme.md +++ b/readme.md @@ -75,7 +75,8 @@ Create a file names `package.json` with the following content: "bin": "./index.js", "type": "module", "dependencies": { - "unified-language-server" + "remark": "^14.0.0", + "unified-language-server": "^1.0.0" } } ``` @@ -83,6 +84,7 @@ Create a file names `package.json` with the following content: Then create `index.js` with the following content: ```js +import {remark} from 'remark' import {createUnifiedLanguageServer} from 'unified-language-server' process.title = 'remark-language-server' @@ -91,7 +93,7 @@ createUnifiedLanguageServer({ ignoreName: '.remarkignore', packageField: 'remarkConfig', pluginPrefix: 'remark', - plugins: ['remark-parse', 'remark-stringify'], + processor: remark, rcName: '.remarkrc' }) ``` diff --git a/test/index.js b/test/index.js index 7eaa3d1..57838a8 100644 --- a/test/index.js +++ b/test/index.js @@ -1,4 +1,6 @@ /** + * @typedef {import('unified').Processor} Processor + * @typedef {import('unified').Plugin} Plugin * @typedef {import('./test-plugin').UnifiedTestPluginOptions} UnifiedTestPluginOptions */ @@ -6,6 +8,7 @@ import {pathToFileURL} from 'node:url' import {spy, stub} from 'sinon' import test from 'tape' +import {unified} from 'unified' import * as exports from 'unified-language-server' import { CodeActionKind, @@ -49,6 +52,7 @@ function createMockConnection() { * @param {string} uri * @param {string} text * @param {UnifiedTestPluginOptions} [pluginOptions] + * @param {Processor} [processor] * @param {string} pluginName * @returns {Promise} */ @@ -56,6 +60,7 @@ function getDiagnostic( uri, text, pluginOptions, + processor, pluginName = './test/test-plugin.js' ) { const connection = createMockConnection() @@ -72,7 +77,8 @@ function getDiagnostic( }) configureUnifiedLanguageServer(connection, documents, { - plugins: [[pluginName, pluginOptions]] + plugins: [[pluginName, pluginOptions]], + processor }) onDidChangeContent.firstCall.firstArg({ @@ -107,6 +113,36 @@ test('onInitialize', (t) => { t.end() }) +test('Custom processor', async (t) => { + const uri = String(pathToFileURL('test.md')) + const diagnostics = await getDiagnostic( + uri, + 'test', + undefined, + unified().use( + /** @type {Plugin} */ ( + () => (ast, file) => { + file.message('custom processor') + } + ) + ) + ) + + t.deepEquals(diagnostics, { + uri, + version: 0, + diagnostics: [ + { + range: {start: {line: 0, character: 0}, end: {line: 0, character: 0}}, + message: 'custom processor', + severity: DiagnosticSeverity.Warning + } + ] + }) + + t.end() +}) + test('onDocumentFormatting different', async (t) => { const connection = createMockConnection() const documents = new TextDocuments(TextDocument) @@ -220,6 +256,7 @@ test('onDidChangeContent transformer error', async (t) => { uri, 'test', undefined, + undefined, 'unresolved-plugin' )