@diplodoc/translation

中文文档
1.10.3发布于 2026/11/09MITTypeScriptCommonJS构建来源已验证近 30 天下载 9479

markdown translation utilities

原始使用文档
文档目录8 节
  1. 📚 Sections
  2. 👋 Introduction
  3. 🌟 Project Overview
  4. 🎯 Purpose and Scope
  5. 🚀 Getting Started
  6. 💻 Installation
  7. 📖 API
  8. 👨‍💻 Usage

NPM version Quality Gate Status Coverage Maintainability Rating Reliability Rating Security Rating

@diplodoc/translation

📚 Sections

👋 Introduction

Hey, There! 🤙🏼

Are you tired of copying and pasting your markdown content into translation tools, seeing markdown syntax instead of translatable text and collisions?

Say no more! 👌

With @diplodoc/translation, you can generate skeleton/XLIFF for CAT tools and compose translated Markdown back.

🌟 Project Overview

🚀💻 This library got you covered with two API interfaces: extract and compose. 💻🚀

Extract text from markdown files 📄 and generate a skeleton/XLF file. 🧑‍💻

Compose the extracted text with the translations from the XLF file to create the translated markdown file. 🌎📄💬

Say goodbye to manual translation work and streamline your workflow with @diplodoc/translation.

🎯 Purpose and Scope

🤯 Hold on to your hats, folks! This library is tackling some serious problems with markdown translations!

🚫 Say goodbye to unnecessary heavy load on translators caused by naive approaches that include markdown syntax constructs into text segments.

💥 And don't even get us started on "collisions" - yikes! But fear not, this library has got your back with a reliable solution for extracting text content from markdown without those pesky collisions.

🕵️‍♀️ How? By parsing and rendering xlf and skeleton, of course! So sit back, relax, and let this library handle all your markdown translation needs.

Say goodbye to those pesky issues and hello to efficient and accurate translations with @diplodoc/translation.

🚀 Getting Started

💻 Installation

npm install @diplodoc/translation

📖 API

🔍 extract ✨

This function extracts skeleton and XLF strings from a given markdown string 📝, to use in the 💻 Computer Assisted Translation tool. 🌟

function extract(parameters: ExtractParameters): ExtractOutput;

🎛️ Parameters Object

NameTypeDescription
markdownstringThe markdown string to extract text content from.
sourceLanguageLocaleThe source language and locale.
targetLanguageLocaleThe target language and locale.
skeletonPathstringThe path where you will put the skeleton file.
markdownPathstringThe path from where the markdown file were read.
filterbooleanFilter none translatable files.
compactbooleanCompact XLIFF without <g> wrappers. Code blocks are processed precisely instead of as a whole. Markup wrapping a whole sentence goes to the skeleton, markup that only starts or ends it stays in the unit; a sentence may start with inline code.
codestringHow fenced code is processed, see below.
unitLocalIdsbooleanRestart placeholder ids (g-N/x-N) for every unit instead of numbering them through the document, so a unit's text does not depend on the markup above it. Use when unit texts serve as translation cache keys. Off by default.

🧱 Code blocks

The code parameter selects how much of a fenced code block is translatable:

ModeWhat goes to translation
noNothing, the block is copied as is.
allThe whole block. Default without compact.
precise<placeholders> of any block and comments of bash/shell/sh blocks. Default with compact.
adaptiveEverything of precise plus line comments of other languages (#, //, --) and labels of mermaid diagrams. Commented-out code, tool directives and separators stay as is.

A single block overrides the mode with its info string: ```yaml translate=adaptive.

📚 Types

🎛️ LanguageLocale Object

NameTypeDescription
languagestringThe language code, as described in ISO 639-1.
localestringThe locale code in alpha-2 format, as described in ISO 3166-1

🎛️ ExtractOutput Object

NameTypeDescription
skeletonstringThe extracted skeleton string.
xlfstringThe extracted XLF string.
warningsstring[]Parts of the markdown left untranslated without failing the extraction, one line each. For example, a page-constructor block whose YAML cannot be parsed.

🔤 compose ✨

This function composes 🔠 translated 📝 markdown from given 🧟‍♂️ skeleton and 💬 XLF.

function compose(parameters: ComposeParameters): string

🎛️ Parameters Object

NameTypeDescription
skeletonstringThe skeleton with hashes instead of translatable text
xlfstringXLF with translated text

👨‍💻 Usage

🔍 Extract

Extract skeleton and xlf from markdown

👀 Example

import {extract} from '@diplodoc/translation';

const markdown = `# Heading 1

Paragraph

- First list item
- Second list item

Heading 2
-----------

Paragraph with **bold text** and *italic*

> text inside of the blockquote

1. First ordered list item
2. Second ordered list item

[Link text](files/file.md "Link Description")

![Alternative image text](files/image.png "Image title")
`;

const params = {
    markdown,
    source: {language: 'en', locale: 'US' as const},
    target: {language: 'ru', locale: 'RU' as const},
    skeletonPath: 'path/where/you/will/put/generated/skeleton.skl.md',
    markdownPath: 'path/to/file/where/markdown/string/came/from.md',
}

const {skeleton, xlf} = extract(params);

console.log(skeleton);
console.log(xlf);

💥 Output

skeleton output

xlf output

🔤 Compose

Compose skeleton and xlf into translated markdown

👀 Example

import {compose} from '@diplodoc/translation';

const skeleton = `%%%1%%%
%%%2%%%
- %%%3%%%
- %%%4%%%
%%%5%%%
-----------
%%%6%%%**%%%7%%%**%%%8%%%*%%%9%%%*
> %%%10%%%
1. %%%11%%%
2. %%%12%%%

[%%%13%%%](files/file.md "%%%14%%%")

![%%%15%%%](files/image.png "%%%16%%%")
`

const xlf = `<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
  <file original="text.md" source-language="ru-RU" target-language="en-US" datatype="markdown">
    <header>
      <skeleton>
        <external-file href="text.skl.md"></external-file>
      </skeleton>
    </header>
    <body>
      <trans-unit id="1">
        <source>Heading 1</source>
        <target>Заголовок 1</target>
      </trans-unit>
      <trans-unit id="2">
        <source>Paragraph</source>
        <target>Параграф</target>
      </trans-unit>
      <trans-unit id="3">
        <source>First list item</source>
        <target>Первый элемент списка</target>
      </trans-unit>
      <trans-unit id="4">
        <source>Second list item</source>
        <target>Второй элемент списка</target>
      </trans-unit>
      <trans-unit id="5">
        <source>Heading 2</source>
        <target>Заголовок 2</target>
      </trans-unit>
      <trans-unit id="6">
        <source>Paragraph with </source>
        <target>Параграф содержащий </target>
      </trans-unit>
      <trans-unit id="7">
        <source>bold text</source>
        <target>жирный текст</target>
      </trans-unit>
      <trans-unit id="8">
        <source> and </source>
        <target> и </target>
      </trans-unit>
      <trans-unit id="9">
        <source>italics</source>
        <target>курсив</target>
      </trans-unit>
      <trans-unit id="10">
        <source>text inside of the blockquote</source>
        <target>текст внутри цитаты</target>
      </trans-unit>
      <trans-unit id="11">
        <source>First ordered list item</source>
        <target>Первый элемент нумерованного списка</target>
      </trans-unit>
      <trans-unit id="12">
        <source>Second ordered list item</source>
        <target>Второй элемент нумерованного списка</target>
      </trans-unit>
      <trans-unit id="13">
        <source>Link text</source>
        <target>Текст ссылки</target>
      </trans-unit>
      <trans-unit id="14">
        <source>Link Description</source>
        <target>Описание ссылки</target>
      </trans-unit>
      <trans-unit id="15">
        <source>Alternative image text</source>
        <target>Альтернативный текст картинки</target>
      </trans-unit>
      <trans-unit id="16">
        <source>Image title</source>
        <target>Титул картинки</target>
      </trans-unit>
    </body>
  </file>
</xliff>
`;

const markdown = compose({skeleton, xlf});

console.log(markdown);

💥 Output

markdown output

版本与发布记录

@diplodoc/translation 共发布 63 个版本,首次发布于 2024/02/21;过去 12 个月发布了 20 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.10.3latest2026/11/093 天前
1.10.22026/10/2814 天前
1.10.12026/09/291 个月前
1.10.02026/09/251 个月前
1.9.12026/09/241 个月前
1.9.02026/09/221 个月前

关于 @diplodoc/translation 的常见问题

@diplodoc/translation 是什么?

markdown translation utilities

如何安装 @diplodoc/translation?

在项目目录运行 npm install @diplodoc/translation,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:markdown-it(^13.0.2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@diplodoc/translation 支持 TypeScript 吗?

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

@diplodoc/translation 支持 ESM 还是 CommonJS?

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

@diplodoc/translation 需要什么版本的 Node.js?

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

@diplodoc/translation 可以商用吗?

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

@diplodoc/translation 的下载量有多少?

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

@diplodoc/translation 还在维护吗?

最新版本 1.10.3 发布于 2026/11/09(3 天前);至今共发布 63 个版本,其中过去 12 个月发布了 20 个;首次发布于 2024/02/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。