@dsanders11/vscode-markdown-languageservice

中文文档
0.3.0发布于 2023/03/18MITTypeScriptCommonJS近 30 天下载 3.9万

Markdown language service

原始使用文档
文档目录4 节
  1. Features
  2. Usage
  3. Additional Links
  4. Contributing

VS Code Markdown Language Service

The language service that powers VS Code's Markdown support, extracted so that it can be reused by other editors and tools.

Features

This library targets CommonMark. Support for other Markdown dialects and extensions is not within the scope of this project.

Currently supported language features:

  • Document links (clickable spans in the editor)

    Supported links include:

    • Links to headers within the current file: [text](#header)
    • Absolute and relative links to files: [text](path/to/file.md)
    • Reference links: [text][link-name]
  • Document symbols

    Finds all headers within a markdown file

  • Workspace symbols

    Find all headers across all markdown files in the workspace.

  • Folding ranges

    Folding ranges are computed for:

    • Header sections
    • Region sections
    • Lists
    • Block elements
  • Smart select (expand selection)

  • Completions

    Supports completions for:

    • Links to headers
    • Path links
    • Reference links
  • Find all references

    Supports finding references to:

    • Headers
    • Path links
    • Fragments in links
    • Reference links
  • Definitions

    Supports finding definitions headers and reference links.

  • Renames

    Supports renaming of headers and links.

  • Organize link definitions.

    Groups and sorts link definitions in a file, optionally also removing unused definitions.

  • Code actions

    • Extract all occurrences of a link in a file to a link definition at the bottom of the file.
    • Quick fixes for removing duplicated or unused link definitions.
  • Diagnostics (error reporting)

    Supports generating diagnostics for invalid links to:

    • References.
    • Header within the current file.
    • Files in the workspace.
    • Headers in other files.

    Also can generate diagnostics for:

    • Unused link definitions.
    • Duplicate link definitions.
  • Update links on file rename

    Generate an edit that updates all links when a file/directory in the workspace is renamed or moved.

Usage

To get started using this library, first install it into your workspace:

npm install vscode-markdown-languageservice

To use the language service, first you need to create an instance of it using createLanguageService. We use dependency injection to allow the language service to be used in as many contexts as possible.

import * as md from 'vscode-markdown-languageservice';

// Implement these
const parser: md.IMdParser = ...;
const workspace: md.IWorkspace = ...;
const logger: md.ILogger = ...;

const languageService = md.createLanguageService({ workspace, parser, logger });

After creating the service, you can ask it for the language features it supports:

// We're using the vscode-language types in this demo
// If you want to use them, make sure to run:
//
//     npm install vscode-languageserver vscode-languageserver-textdocument
//
// However you can also bring your own types if you want to instead.

import { CancellationTokenSource } from 'vscode-languageserver';
import { TextDocument } from 'vscode-languageserver-textdocument';

const cts = new CancellationTokenSource();

// Create a virtual document that holds our file content
const myDocument = TextDocument.create(
    URI.file('/path/to/file.md').toString(), // file path
    'markdown', // file language
    1, // version
    [ // File contents
        '# Hello',
        'from **Markdown**',
        '',
        '## World!',
    ].join('\n')
);

const symbols = await languageService.getDocumentSymbols(myDocument, { includeLinkDefinitions: true }, cts.token);

See example.cjs for complete, minimal example of using the language service. You can run in using node example.cjs.

Contributing

If you're interested in contributing

  1. Clone this repo
  2. Install dependencies using npm install
  3. Start compilation using npm run watch

You can run the unit tests using npm test or by opening the project in VS Code and pressing F5 to debug.

版本与发布记录

@dsanders11/vscode-markdown-languageservice 共发布 3 个版本,首次发布于 2022/12/03;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.3.0latest2023/03/183 年前
0.3.0-alpha.42022/12/073 年前
0.3.0-alpha.32022/12/033 年前

关于 @dsanders11/vscode-markdown-languageservice 的常见问题

@dsanders11/vscode-markdown-languageservice 是什么?

Markdown language service

如何安装 @dsanders11/vscode-markdown-languageservice?

在项目目录运行 npm install @dsanders11/vscode-markdown-languageservice,或使用 pnpm add / yarn add / bun add。

@dsanders11/vscode-markdown-languageservice 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dsanders11/vscode-markdown-languageservice 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@dsanders11/vscode-markdown-languageservice 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js *。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@dsanders11/vscode-markdown-languageservice 可以商用吗?

@dsanders11/vscode-markdown-languageservice 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dsanders11/vscode-markdown-languageservice 的下载量有多少?

根据 npm 官方下载统计,@dsanders11/vscode-markdown-languageservice 在 2026/10/13 – 2026/11/11 这 30 天内被下载 39,092 次,日均约 1,303 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dsanders11/vscode-markdown-languageservice 还在维护吗?

最新版本 0.3.0 发布于 2023/03/18(3 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/12/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。