From 26df933ea1bebc50cfa55ff3f18ada385c51fa10 Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 15:59:18 +0800 Subject: [PATCH 01/21] support all wordless by default --- CHANGELOG.md | 3 +- data.ts | 100 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 62 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e1bf5a..56bd24b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ -## v0.1.1 +## v0.2.0 - Add MIT License to node manifest. - GitHub Actions for check. - Readme manifest details. +- Support all registered wordless languages by default. ## v0.1.0 diff --git a/data.ts b/data.ts index fc56f63..b0cb7eb 100644 --- a/data.ts +++ b/data.ts @@ -30,46 +30,6 @@ export type Options = { supportWordless: LanguageRanges[] } -/** - * @param code unicode number of a character. - * @param options {@link Options} for the wordless languages and - * a series of non-wordless languages for optimization. - * @returns Index of the character in the given wordless language series, - * if there's not {@link Range} contains such code, - * it means this is not a character of a wordless language, - * and it will return -1. And if it's an emoji, it will return -2. - */ -export function langIndexOf(code: number, options?: Options): number { - options = { - optimizeWords: options?.optimizeWords ?? [commonWords], - supportWordless: options?.supportWordless ?? [], - } - - // Process optimizations. - for (const ranges of options!.optimizeWords!) { - for (const range of ranges) { - if (code >= range[0] && code <= range[1]) return -1 - } - } - - // Process Emoji. - for (const ranges of emoji) { - for (const range of ranges) { - if (code >= range[0] && code <= range[1]) return -2 - } - } - - // Process wordless language index. - const wordless = options!.supportWordless! - for (let index = 0; index < wordless.length; index++) { - const ranges = wordless[index] - for (const range of ranges) { - if (code >= range[0] && code <= range[1]) return index - } - } - return -1 -} - /** Unicode from zero to 0x0dff, commonly used language with words. */ export const commonWords: LanguageRanges = [[0x0000, 0x0dff]] @@ -192,3 +152,63 @@ export const cuneiform: LanguageRanges = [ /** Ancient Egyptian hieroglyphs. */ export const hieroglyphics: LanguageRanges = [[0x13000, 0x1345f]] + +/** Enable optimization for all registered wordless languages. */ +export const allWordless: LanguageRanges[] = [ + chineseAndJapanese, + tibetan, + thai, + lao, + cambodian, + burmese, + yi, + dehongDai, + xishuangbannaNewDai, + xishuangbannaOldDai, + jiangyongWomanScript, + oldChinesePinyin, + khitanSmallScript, + tangut, + cuneiform, + hieroglyphics, +] + +/** + * @param code unicode number of a character. + * @param options {@link Options} for the wordless languages and + * a series of non-wordless languages for optimization. + * @returns Index of the character in the given wordless language series, + * if there's not {@link Range} contains such code, + * it means this is not a character of a wordless language, + * and it will return -1. And if it's an emoji, it will return -2. + */ +export function langIndexOf(code: number, options?: Options): number { + options = { + optimizeWords: options?.optimizeWords ?? [commonWords], + supportWordless: options?.supportWordless ?? allWordless, + } + + // Process optimizations. + for (const ranges of options!.optimizeWords!) { + for (const range of ranges) { + if (code >= range[0] && code <= range[1]) return -1 + } + } + + // Process Emoji. + for (const ranges of emoji) { + for (const range of ranges) { + if (code >= range[0] && code <= range[1]) return -2 + } + } + + // Process wordless language index. + const wordless = options!.supportWordless! + for (let index = 0; index < wordless.length; index++) { + const ranges = wordless[index] + for (const range of ranges) { + if (code >= range[0] && code <= range[1]) return index + } + } + return -1 +} From 6a30e5355cce6f4914991baedeba5a950f742e53 Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 15:59:43 +0800 Subject: [PATCH 02/21] rm unnecessary .npmrc --- .npmrc | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .npmrc diff --git a/.npmrc b/.npmrc deleted file mode 100644 index ae64359..0000000 --- a/.npmrc +++ /dev/null @@ -1 +0,0 @@ -//registry.npmjs.org/:_authToken=${NPM_TOKEN} From d2fabe09782c888f52196bc5db8db504b7157c8c Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 16:00:54 +0800 Subject: [PATCH 03/21] doc example without options --- README.md | 5 ++--- index.ts | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c61c4af..6d65b7d 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,6 @@ you can enable the softbreak for English but disable it for Chinese by following configurations: ```ts -import {Options, chineseAndJapanese, wordless} from "markdown-it-wordless" - -md.use(wordless, {supportWordless: [chineseAndJapanese]}) +import {Options} from "markdown-it-wordless" +md.use(wordless) ``` diff --git a/index.ts b/index.ts index 5118940..4d52a94 100644 --- a/index.ts +++ b/index.ts @@ -17,8 +17,8 @@ const space = " " * For example, if you are using Chinese or Japanese with English, * you may consider code like this: * ```ts - * import {wordless, chineseAndJapanese, Options} from 'markdown-it-wordless' - * md.use(wordless, {supportWordless: [chineseAndJapanese]}) + * import {wordless} from 'markdown-it-wordless' + * md.use(wordless) * ``` */ export function wordless(md: md, options?: Options) { From 189789784c394aedec669413b851b0a03b920607 Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 16:22:28 +0800 Subject: [PATCH 04/21] enable vitest in source --- index.ts | 10 ++++++++++ package.json | 8 ++++++-- rollup.config.js | 6 ++++++ vitest.config.ts | 2 ++ 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 vitest.config.ts diff --git a/index.ts b/index.ts index 4d52a94..c944e6d 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,5 @@ import md from "markdown-it" +import {describe, expect, test} from "vitest" import type {Options} from "./data" import {langIndexOf} from "./data" @@ -31,3 +32,12 @@ export function wordless(md: md, options?: Options) { return before === after && before !== -1 && before != -2 ? "" : space } } + +// @ts-ignore +if (import.meta.vitest) { + describe("validate", function () { + test("placeholder", function () { + expect(1 + 1).toBe(2) + }) + }) +} diff --git a/package.json b/package.json index a1db9ca..bb19efd 100644 --- a/package.json +++ b/package.json @@ -21,12 +21,15 @@ "format.check": "prettier . --check", "format": "prettier . --write", "build": "rollup --config rollup.config.js", - "review": "npm run format.check && npm run build" + "review": "npm run format.check && npm test && npm run build", + "prepublishOnly": "npm run review", + "test": "vitest run" }, "dependencies": { "markdown-it": "^14.1.0" }, "devDependencies": { + "@rollup/plugin-replace": "^5.0.7", "@rollup/plugin-terser": "^0.4.4", "@rollup/plugin-typescript": "^11.1.6", "@types/markdown-it": "^14.1.1", @@ -34,6 +37,7 @@ "rollup": "^4.18.0", "rollup-plugin-dts": "^6.1.1", "tslib": "^2.6.3", - "typescript": "^5.5.2" + "typescript": "^5.5.2", + "vitest": "^1.6.0" } } diff --git a/rollup.config.js b/rollup.config.js index db9ee99..34dca12 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,17 +1,22 @@ +import replace from "@rollup/plugin-replace" import terser from "@rollup/plugin-terser" import typescript from "@rollup/plugin-typescript" import {defineConfig} from "rollup" import dts from "rollup-plugin-dts" +const external = ["vitest"] + export default defineConfig([ { plugins: [ + replace({"import.meta.vitest": "undefined", preventAssignment: true}), typescript({ compilerOptions: {allowSyntheticDefaultImports: true}, sourceMap: true, }), terser(), ], + external, input: "index.ts", output: [ {file: "index.js", format: "esm", sourcemap: true}, @@ -20,6 +25,7 @@ export default defineConfig([ }, { plugins: [dts({compilerOptions: {allowSyntheticDefaultImports: true}})], + external, input: "index.ts", output: {file: "index.d.ts", format: "esm"}, }, diff --git a/vitest.config.ts b/vitest.config.ts new file mode 100644 index 0000000..73bf9ad --- /dev/null +++ b/vitest.config.ts @@ -0,0 +1,2 @@ +import {defineConfig} from "vitest/config" +export default defineConfig({test: {includeSource: ["*.ts"]}}) From b4d5467aff06366978ed3c52d51fcd99fca81ad7 Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 16:28:24 +0800 Subject: [PATCH 05/21] rm unnecessary external const --- rollup.config.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/rollup.config.js b/rollup.config.js index 34dca12..7a45c19 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -4,8 +4,6 @@ import typescript from "@rollup/plugin-typescript" import {defineConfig} from "rollup" import dts from "rollup-plugin-dts" -const external = ["vitest"] - export default defineConfig([ { plugins: [ @@ -16,7 +14,7 @@ export default defineConfig([ }), terser(), ], - external, + external: ["vitest"], input: "index.ts", output: [ {file: "index.js", format: "esm", sourcemap: true}, @@ -25,7 +23,6 @@ export default defineConfig([ }, { plugins: [dts({compilerOptions: {allowSyntheticDefaultImports: true}})], - external, input: "index.ts", output: {file: "index.d.ts", format: "esm"}, }, From 83218970f4892bf56d67b8092a5d62508185b394 Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 16:28:30 +0800 Subject: [PATCH 06/21] update version code --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bb19efd..3691814 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "markdown-it-wordless", "description": "A markdown-it plugin for wordless languages optimization.", - "version": "0.1.1", + "version": "0.2.0", "type": "module", "license": "MIT", "author": { From 687a592ee1c906424ccd3da192a21d8d8d0127f6 Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 16:32:29 +0800 Subject: [PATCH 07/21] enable github dependabot --- .github/dependabot.yaml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .github/dependabot.yaml diff --git a/.github/dependabot.yaml b/.github/dependabot.yaml new file mode 100644 index 0000000..0f47db1 --- /dev/null +++ b/.github/dependabot.yaml @@ -0,0 +1,5 @@ +version: 2 +updates: + - package-ecosystem: npm + directory: "/" + schedule: {interval: weekly} From 19e33d84e5f923832172f6b5b34a2807d49280f2 Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 16:33:27 +0800 Subject: [PATCH 08/21] rm unnecessary nest --- index.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/index.ts b/index.ts index c944e6d..71f4898 100644 --- a/index.ts +++ b/index.ts @@ -1,5 +1,5 @@ import md from "markdown-it" -import {describe, expect, test} from "vitest" +import {expect, test} from "vitest" import type {Options} from "./data" import {langIndexOf} from "./data" @@ -35,9 +35,7 @@ export function wordless(md: md, options?: Options) { // @ts-ignore if (import.meta.vitest) { - describe("validate", function () { - test("placeholder", function () { - expect(1 + 1).toBe(2) - }) + test("placeholder", function () { + expect(1 + 1).toBe(2) }) } From 1708811ff6701b5419ac38bf792e737e425f37f4 Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 16:49:03 +0800 Subject: [PATCH 09/21] test validation --- index.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/index.ts b/index.ts index 71f4898..c5ed46c 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,5 @@ import md from "markdown-it" +import MarkdownIt from "markdown-it/index.mjs" import {expect, test} from "vitest" import type {Options} from "./data" @@ -35,7 +36,10 @@ export function wordless(md: md, options?: Options) { // @ts-ignore if (import.meta.vitest) { - test("placeholder", function () { - expect(1 + 1).toBe(2) + test("basic function", function () { + const raw = "English\nにほんご\n中文\nབོད་ཡིག།" + expect(new MarkdownIt().use(wordless).render(raw)).toBe( + "

English にほんご中文 བོད་ཡིག།

\n", + ) }) } From ff93b4bf751da23c5b90d88549a7725924843c56 Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 16:49:43 +0800 Subject: [PATCH 10/21] update changelog --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56bd24b..6f63e3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,10 @@ ## v0.2.0 - Add MIT License to node manifest. -- GitHub Actions for check. -- Readme manifest details. +- GitHub Actions for check and dependabot. - Support all registered wordless languages by default. +- Vitest to validate basic functions. +- Readme manifest details. ## v0.1.0 From bb1738fdebdcc9b92d887d07529c103487374cb5 Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 17:15:50 +0800 Subject: [PATCH 11/21] doc in details --- README.md | 62 +++++++++++++++++++++++++++++++++++++++++----------- package.json | 2 +- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 6d65b7d..c2bec42 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,63 @@ # Markdown-it Wordless -A markdown-it plugin to optimize wordless multi-language space render. +A markdown-it plugin to optimize wordless multi-language line-break render. -When a paragraph is long in markdown, we usually separate them into lines. +When a paragraph is long in markdown, we usually separate them into lines, +and it will finally be rendered into a single line inside HTML. But for wordless languages (such as Chinese and Japanese), -an extra line break will cause an unnecessary white space. -You can definitely set: +they do not use spaces to separate words, +that they don't need a space to be added when processing line-break. + +If you are only working with a single wordless language, +you can definitely use the following code, +which will disable all spaces when line break +(render single `\n` into an empty string rather than a space): ```ts md.renderer.rules.softbreak = () => "" ``` -to disable all spaces when line break, -but how about the condition when resolving multi-languages? - -You can use this plugin to resolve the problem. -In this plugin, you can config in details -to resolve line break in multi-languages. -For example, when working with Chinese and English, -you can enable the softbreak for English but disable it for Chinese -by following configurations: +But once working with multi-languages, +especially when there's a mix of wordless and wordful languages, +such as using Chinese and English in a single markdown document, +such options cannot handle all cases. +So here comes this `"markdown-it-wordless"` plugin, +and you can use it like this: ```ts import {Options} from "markdown-it-wordless" md.use(wordless) ``` + +## Basic functions + +1. Wordful languages (such as English and Arabic) will be rendered as usual. +2. It won't add a space when line break between the same wordless language. +3. It will add a space when line break between different wordless languages. +4. Specially, Chinese and Japanese will be treated as a same language, + as there are many shared characters between them, + and their character styles are almost the same. +5. Although Korean characters are like Chinese and Japanese (CJK), + Korean is not a wordless language, it uses spaces to separate words. + +## Use it with VitePress + +[VitePress](https://vitepress.dev) is an excellent static site generator, +and this package is also inspired when the author using VitePress. +It's strongly recommended to add such plugin to VitePress +if you are using wordless languages. And here's how to config: + +```ts +// /.vitepress/config.ts +import {defineConfig} from "vitepress" +import {wordless} from "markdown-it-wordless" + +export default defineConfig({ + markdown: { + config(md) { + md.use(wordless) + }, + }, + // Other configs... +}) +``` diff --git a/package.json b/package.json index 3691814..c74f900 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "markdown-it-wordless", - "description": "A markdown-it plugin for wordless languages optimization.", + "description": "A markdown-it plugin for wordless languages line-break.", "version": "0.2.0", "type": "module", "license": "MIT", From 06ee9a9044b0b48d0e81c643a819929f33beea8f Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 17:17:18 +0800 Subject: [PATCH 12/21] test same wordless language inside --- index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.ts b/index.ts index c5ed46c..895cab4 100644 --- a/index.ts +++ b/index.ts @@ -37,9 +37,9 @@ export function wordless(md: md, options?: Options) { // @ts-ignore if (import.meta.vitest) { test("basic function", function () { - const raw = "English\nにほんご\n中文\nབོད་ཡིག།" + const raw = "English\nにほんご\n中文\n中文\nབོད་ཡིག།\nབོད་ཡིག།" expect(new MarkdownIt().use(wordless).render(raw)).toBe( - "

English にほんご中文 བོད་ཡིག།

\n", + "

English にほんご中文中文 བོད་ཡིག།བོད་ཡིག།

\n", ) }) } From be86e8d8df94fe72c292866706d562ff11b976e9 Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 17:24:05 +0800 Subject: [PATCH 13/21] rollup external in details --- rollup.config.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/rollup.config.js b/rollup.config.js index 7a45c19..233c699 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -14,7 +14,13 @@ export default defineConfig([ }), terser(), ], - external: ["vitest"], + external(id) { + const prefixes = ["vitest", "markdown-it"] + for (const prefix of prefixes) { + if (id.startsWith(prefix)) return true + } + return false + }, input: "index.ts", output: [ {file: "index.js", format: "esm", sourcemap: true}, From fc8458a92905fd97b34c70e8fb21e0beecdc42bc Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 17:31:31 +0800 Subject: [PATCH 14/21] fix vitest import meta --- index.ts | 3 +-- rollup.config.js | 2 +- tsconfig.json | 10 ++++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 tsconfig.json diff --git a/index.ts b/index.ts index 895cab4..c4156f5 100644 --- a/index.ts +++ b/index.ts @@ -1,6 +1,5 @@ import md from "markdown-it" import MarkdownIt from "markdown-it/index.mjs" -import {expect, test} from "vitest" import type {Options} from "./data" import {langIndexOf} from "./data" @@ -34,8 +33,8 @@ export function wordless(md: md, options?: Options) { } } -// @ts-ignore if (import.meta.vitest) { + const {expect, test} = import.meta.vitest test("basic function", function () { const raw = "English\nにほんご\n中文\n中文\nབོད་ཡིག།\nབོད་ཡིག།" expect(new MarkdownIt().use(wordless).render(raw)).toBe( diff --git a/rollup.config.js b/rollup.config.js index 233c699..2e0bf4e 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -15,7 +15,7 @@ export default defineConfig([ terser(), ], external(id) { - const prefixes = ["vitest", "markdown-it"] + const prefixes = ["markdown-it"] for (const prefix of prefixes) { if (id.startsWith(prefix)) return true } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..4b5d5d8 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "module": "ESNext", + "target": "ESNext", + "esModuleInterop": true, + "types": ["vitest/importMeta"] + }, + "include": ["index.ts"], + "exclude": ["node_modules", "*.js", "*.mjs"] +} From c73390c4182dfc6ff6f79c51ada1325b67bad0e0 Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 17:33:37 +0800 Subject: [PATCH 15/21] npm ignore vitest config file --- .npmignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.npmignore b/.npmignore index 1d5fb8c..c3620b8 100644 --- a/.npmignore +++ b/.npmignore @@ -9,6 +9,7 @@ yarn.lock .gitattributes .prettierrc.yaml rollup.config.js +vitest.config.ts # Platform specified files. .DS_Store From e7c0221a22585d73e6f7dc002eb33a8afd726db6 Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 17:35:00 +0800 Subject: [PATCH 16/21] update version code --- CHANGELOG.md | 2 +- package.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f63e3f..e5b37bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## v0.2.0 +## v1.0.0 - Add MIT License to node manifest. - GitHub Actions for check and dependabot. diff --git a/package.json b/package.json index c74f900..db68b2c 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,15 @@ { "name": "markdown-it-wordless", "description": "A markdown-it plugin for wordless languages line-break.", - "version": "0.2.0", + "version": "1.0.0", "type": "module", "license": "MIT", + "homepage": "https://github.com/treeinfra/markdown-it-wordless", "author": { "name": "James Aprosail", "email": "aprosail@outlook.com", "url": "https://github.com/aprosail" }, - "homepage": "https://github.com/treeinfra/markdown-it-wordless", "exports": { ".": { "require": "./index.cjs", From db66a577afe2e3ae3923a2f94e71b3c23e94dc27 Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 17:36:28 +0800 Subject: [PATCH 17/21] doc add md-it imports --- README.md | 2 ++ index.ts | 1 + 2 files changed, 3 insertions(+) diff --git a/README.md b/README.md index c2bec42..cc4946f 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ which will disable all spaces when line break (render single `\n` into an empty string rather than a space): ```ts +import md from "markdown-it" md.renderer.rules.softbreak = () => "" ``` @@ -25,6 +26,7 @@ So here comes this `"markdown-it-wordless"` plugin, and you can use it like this: ```ts +import md from "markdown-it" import {Options} from "markdown-it-wordless" md.use(wordless) ``` diff --git a/index.ts b/index.ts index c4156f5..94b9161 100644 --- a/index.ts +++ b/index.ts @@ -18,6 +18,7 @@ const space = " " * For example, if you are using Chinese or Japanese with English, * you may consider code like this: * ```ts + * import md from "markdown-it" * import {wordless} from 'markdown-it-wordless' * md.use(wordless) * ``` From 4bb1a6b8ce53e8665a8015090589668fadff8f64 Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 17:45:53 +0800 Subject: [PATCH 18/21] fix code docs --- data.ts | 13 +++++++++---- index.ts | 29 +++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/data.ts b/data.ts index b0cb7eb..998d84f 100644 --- a/data.ts +++ b/data.ts @@ -8,10 +8,15 @@ export type Range = [number, number] export type LanguageRanges = NonEmptyArray /** - * The default value is empty, you need to add it manually. - * Parsing wordless languages costs a lot. - * It's strongly recommended to only introduce the required series. - * For example: + * The default value will enable all languages registered inside + * the {@link allWordless} const, and enable optimization for + * {@link commonWords} by default. + * + * If you'd like to customize the support languages to improve performance, + * you can config like the following example: + * The following example only enables wordless languages optimization + * for Chinese and Japanese, all other wordless languages will be omitted. + * * ```ts * import {wordless, chineseAndJapanese, Options} from 'markdown-it-wordless' * md.use(wordless, {supportWordless: [chineseAndJapanese]}) diff --git a/index.ts b/index.ts index 94b9161..0295174 100644 --- a/index.ts +++ b/index.ts @@ -10,18 +10,35 @@ export * from "./data" const space = " " /** - * The default {@link Options} contains no wordless languages, - * that you need to add required optimization manually. - * Render wordless languages cost a lot, - * it's recommended to only add required language ranges. + * A markdown-it plugin to optimize wordless multi-language line-break render. + * See [readme](./README.md) of this package for more details. + * Here's the minimal examples on how to use it: * - * For example, if you are using Chinese or Japanese with English, - * you may consider code like this: * ```ts * import md from "markdown-it" * import {wordless} from 'markdown-it-wordless' * md.use(wordless) * ``` + * + * ## For VitePress users + * + * If you are using [VitePress](https://vitepress.dev), + * you may config like this: + * + * ```ts + * // /.vitepress/config.ts + * import {defineConfig} from "vitepress" + * import {wordless} from "markdown-it-wordless" + * + * export default defineConfig({ + * markdown: { + * config(md) { + * md.use(wordless) + * }, + * }, + * // Other configs... + * }) + * ``` */ export function wordless(md: md, options?: Options) { md.renderer.rules.softbreak = function (tokens, index) { From 5d544dc84e928872244ac7ad67d97c08d4d0a800 Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 18:03:04 +0800 Subject: [PATCH 19/21] doc for customization --- README.md | 27 +++++++++++++++++++++++++++ data.ts | 12 ++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cc4946f..759de02 100644 --- a/README.md +++ b/README.md @@ -63,3 +63,30 @@ export default defineConfig({ // Other configs... }) ``` + +## Customize to optimize performance + +The default option will enable optimization +for all registered wordless languages inside this package. +If you want to optimize performance, +you can specify what exactly wordless language you are using. +You may also specify what wordful language you are using, +because there's only optimization for wordful languages +which unicode is less than `0x0dff`. + +Here's a simple example +if you will only use Chinese or Japanese as wordless languages: + +```ts +import md from "markdown-it" +import {wordless, chineseAndJapanese, Options} from "markdown-it-wordless" +md.use(wordless, {supportWordless: [chineseAndJapanese]}) +``` + +Such optimization is unnecessary in most cases, +because this plugin will not slow down the rendering process a lot +in common cases (only a few milliseconds). +And if you do want to customize, +please make sure you've understand the source code. +Please refer to [`data.ts`](./data.ts) for more details, +and here's documentation for each item in details. diff --git a/data.ts b/data.ts index 998d84f..c9c08f0 100644 --- a/data.ts +++ b/data.ts @@ -1,10 +1,18 @@ /** Ensure an array is not empty. */ type NonEmptyArray = [T, ...T[]] -/** A range of unicode numbers, mark its begin and end, the end is included. */ +/** + * A range of unicode numbers, + * mark its begin and end. + * The end is included (using `<=` rather than `<` in source code). + */ export type Range = [number, number] -/** Unicode {@link Range}s of a single language. */ +/** + * Unicode {@link Range}s of a single language. + * It is a non-empty array of {@link Range} + * because a single language might contains multiple ranges in unicode. + */ export type LanguageRanges = NonEmptyArray /** From 4914d28b23429b5b3b804f36c47e484aa2f33923 Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 18:08:33 +0800 Subject: [PATCH 20/21] fix merge old chinese pinyin into chinese series --- data.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/data.ts b/data.ts index c9c08f0..57871bf 100644 --- a/data.ts +++ b/data.ts @@ -77,7 +77,9 @@ export const chineseAndJapanese: LanguageRanges = [ // [0x3040, 0x309f], // 日文平假名/平仮名ひらがな // [0x30a0, 0x30ff], // 日文片假名/片仮名カタカナ [0x3040, 0x30ff], + [0x3100, 0x312f], // 传统拼音注音符号(ㄆㄧㄣ ㄧㄣ) [0x3190, 0x319f], // 甲乙丙丁天地人... + [0x31a0, 0x31bf], // 传统拼音注音字母(ㄆㄧㄣ ㄧㄣ) // [0x31c0, 0x31ef], // 笔画/筆画 // [0x31f0, 0x31ff], // 日文片假名扩展/片仮名カタカナの拡張 @@ -141,12 +143,6 @@ export const xishuangbannaOldDai: LanguageRanges = [[0x1a20, 0x1aaf]] /** 江永女书 */ export const jiangyongWomanScript: LanguageRanges = [[0x1b170, 0x1b2ff]] -/** 旧版拼音 */ -export const oldChinesePinyin: LanguageRanges = [ - [0x3100, 0x312f], - [0x31a0, 0x31bf], -] - /** 契丹小字 */ export const khitanSmallScript: LanguageRanges = [[0x18b00, 0x18cff]] @@ -179,7 +175,6 @@ export const allWordless: LanguageRanges[] = [ xishuangbannaNewDai, xishuangbannaOldDai, jiangyongWomanScript, - oldChinesePinyin, khitanSmallScript, tangut, cuneiform, From 943edc308efe28e0a659359268e3c1f1dc7ff8a0 Mon Sep 17 00:00:00 2001 From: James Aprosail Date: Sun, 23 Jun 2024 18:17:55 +0800 Subject: [PATCH 21/21] docs about the languages --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 759de02..9c5e2ac 100644 --- a/README.md +++ b/README.md @@ -90,3 +90,21 @@ And if you do want to customize, please make sure you've understand the source code. Please refer to [`data.ts`](./data.ts) for more details, and here's documentation for each item in details. + +## About the supported languages + +You can find all supported languages +in the source code of [`data.ts`](./data.ts). +Each language or language series is an exported const +that you can import and call. + +The languages series are based on the [Unicode](https://unicode.org/charts/). +Most of the languages are coded manually and some of them are +generated by several AI models. So that there might be mistakes, +and the author cannot guarantee the accuracy of the data +because it's almost impossible for a single person to learn all such languages. + +If you are native speaker of one of the those wordless languages +and you find there are some mistakes, +or if there's even some wordless languages not included in this package, +please feel free to open an issue.