diff --git a/docs/guide/plugins.md b/docs/guide/plugins.md index cb9b5b4..65a016d 100644 --- a/docs/guide/plugins.md +++ b/docs/guide/plugins.md @@ -70,6 +70,7 @@ Apps may import from [`markdown-exit/core`](/guide/quick-start.html#core-entry) - Use `import type` for types. If your plugin needs runtime values such as `Token`, import them from `markdown-exit/core`, which shares the same classes as `markdown-exit`. Importing `markdown-exit` at runtime adds linkify-it to the bundle of every app that uses your plugin. - `md.linkify` is `undefined` in the core entry until the app assigns a linkify-it instance. If your plugin accesses `md.linkify`, guard against that or document that the plugin requires linkify-it. +- The same goes for `md.utils.lib.ucmicro`, which throws in the core entry until the app sets it. A plugin that needs uc.micro can import it directly. ### Async Render Rules diff --git a/docs/guide/quick-start.md b/docs/guide/quick-start.md index fceee5b..08070d6 100644 --- a/docs/guide/quick-start.md +++ b/docs/guide/quick-start.md @@ -143,3 +143,13 @@ md.linkify = new LinkifyIt() > [!WARNING] > - Plugins that access `md.linkify` (e.g. `md.linkify.set(...)`) throw in the core entry unless you assign a linkify-it instance before calling `md.use()`. > - If any module in your app imports `markdown-exit` instead of `markdown-exit/core`, linkify-it is bundled anyway, and instances created from `markdown-exit/core` get a linkify-it instance too. + +The core entry also leaves out [uc.micro](https://github.com/markdown-it/uc.micro), which markdown-exit only re-exports for plugins as `md.utils.lib.ucmicro`. Reading it throws in the core entry. If a plugin needs it, such as [markdown-it-abbr](https://github.com/markdown-it/markdown-it-abbr), install `uc.micro` and set it before calling `md.use()`: + +```ts +import { createMarkdownExit } from 'markdown-exit/core' +import * as ucmicro from 'uc.micro' + +const md = createMarkdownExit() +md.utils.lib.ucmicro = ucmicro +``` diff --git a/packages/markdown-exit/src/common/utils.ts b/packages/markdown-exit/src/common/utils.ts index 0ff6f24..221ca44 100644 --- a/packages/markdown-exit/src/common/utils.ts +++ b/packages/markdown-exit/src/common/utils.ts @@ -1,8 +1,8 @@ // Utilities // +import type * as UcMicro from 'uc.micro' import * as mdurl from 'mdurl' -import * as ucmicro from 'uc.micro' import { decodeNamedEntity } from './entities' type ClassToWrap = new (...args: any[]) => object @@ -166,9 +166,10 @@ export function isWhiteSpace(code: number): boolean { return false } -// Currently without astral characters support. +const PUNCT_RE = /[\p{P}\p{S}]/u + export function isPunctChar(ch: string): boolean { - return ucmicro.P.test(ch) || ucmicro.S.test(ch) + return PUNCT_RE.test(ch) } export function isPunctCharCode(code: number): boolean { @@ -295,4 +296,16 @@ export function isPromiseLike(v: any): v is Promise { * so plugins won't have to depend on them explicitly, which reduces their * bundled size (e.g. a browser build). */ -export const lib = { mdurl, ucmicro } +let ucmicro: typeof UcMicro | undefined +export const lib = { + mdurl, + // `markdown-exit` sets uc.micro on import. `markdown-exit/core` leaves it to the app. + get ucmicro(): typeof UcMicro { + if (!ucmicro) + throw new Error('markdown-exit/core does not include uc.micro. Set `md.utils.lib.ucmicro = ucmicro` with `import * as ucmicro from \'uc.micro\'` before using plugins that read it.') + return ucmicro + }, + set ucmicro(module: typeof UcMicro) { + ucmicro = module + }, +} diff --git a/packages/markdown-exit/src/index.ts b/packages/markdown-exit/src/index.ts index d5abee2..94ba636 100644 --- a/packages/markdown-exit/src/index.ts +++ b/packages/markdown-exit/src/index.ts @@ -1,7 +1,10 @@ import { LinkifyIt } from 'linkify-it' +import * as ucmicro from 'uc.micro' +import { lib } from './common/utils' import { setLinkifyFactory } from './parser/linkify-it' setLinkifyFactory(() => new LinkifyIt()) +lib.ucmicro = ucmicro export * from './index.core' export { default } from './index.core' diff --git a/packages/markdown-exit/tests/core-entry.test.ts b/packages/markdown-exit/tests/core-entry.test.ts index 91aef68..4df498e 100644 --- a/packages/markdown-exit/tests/core-entry.test.ts +++ b/packages/markdown-exit/tests/core-entry.test.ts @@ -1,6 +1,7 @@ import type { Rollup } from 'vite' import { fileURLToPath } from 'node:url' import { LinkifyIt } from 'linkify-it' +import * as ucmicro from 'uc.micro' import { build } from 'vite' import { assert, describe, it } from 'vitest' import MarkdownExit from '../src/index.core' @@ -24,6 +25,20 @@ describe('markdown-exit/core', () => { assert.notInclude(await bundleImports('../src/index.core.ts'), 'linkify-it') }) + it('does not bundle uc.micro', async () => { + assert.include(await bundleImports('../src/index.ts'), 'uc.micro') + assert.notInclude(await bundleImports('../src/index.core.ts'), 'uc.micro') + }) + + it('needs uc.micro for `md.utils.lib.ucmicro`', () => { + const md = MarkdownExit() + + assert.throws(() => md.utils.lib.ucmicro, /uc\.micro/) + + md.utils.lib.ucmicro = ucmicro + assert.strictEqual(md.utils.lib.ucmicro.P, ucmicro.P) + }) + it('renders without linkify-it', () => { const md = MarkdownExit()