Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/guide/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions docs/guide/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
21 changes: 17 additions & 4 deletions packages/markdown-exit/src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -295,4 +296,16 @@ export function isPromiseLike<T = unknown>(v: any): v is Promise<T> {
* 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
},
}
3 changes: 3 additions & 0 deletions packages/markdown-exit/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
15 changes: 15 additions & 0 deletions packages/markdown-exit/tests/core-entry.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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()

Expand Down
Loading