diff --git a/docs/guide/quick-start.md b/docs/guide/quick-start.md index fceee5b..2f78923 100644 --- a/docs/guide/quick-start.md +++ b/docs/guide/quick-start.md @@ -143,3 +143,17 @@ 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. + +## Parser Entry + +`markdown-exit/parser` exports `Parser` and `Token` without the renderer and presets, for apps that only need tokens. `Parser` parses with the same rules as the default preset and leaves out linkify-it like the core entry, but has no `use()`, `set()`, `configure()` or `render()`. Change its options and rules directly: + +```ts +import { Parser } from 'markdown-exit/parser' + +const parser = new Parser() +parser.options.html = true +parser.block.ruler.disable('table') + +const tokens = parser.parse('# Hello') +``` diff --git a/packages/markdown-exit/package.json b/packages/markdown-exit/package.json index b10f64a..04c91a0 100644 --- a/packages/markdown-exit/package.json +++ b/packages/markdown-exit/package.json @@ -23,6 +23,7 @@ "exports": { ".": "./dist/index.js", "./core": "./dist/core.js", + "./parser": "./dist/parser.js", "./package.json": "./package.json" }, "main": "./dist/index.js", diff --git a/packages/markdown-exit/src/index.parser.ts b/packages/markdown-exit/src/index.parser.ts new file mode 100644 index 0000000..7b5d373 --- /dev/null +++ b/packages/markdown-exit/src/index.parser.ts @@ -0,0 +1,2 @@ +export * from './parser' +export * from './token' diff --git a/packages/markdown-exit/tests/core-entry.test.ts b/packages/markdown-exit/tests/core-entry.test.ts index 91aef68..98c1091 100644 --- a/packages/markdown-exit/tests/core-entry.test.ts +++ b/packages/markdown-exit/tests/core-entry.test.ts @@ -4,8 +4,9 @@ import { LinkifyIt } from 'linkify-it' import { build } from 'vite' import { assert, describe, it } from 'vitest' import MarkdownExit from '../src/index.core' +import { Parser } from '../src/index.parser' -async function bundleImports(entry: string): Promise { +async function bundle(entry: string): Promise { const result = await build({ configFile: false, logLevel: 'silent', @@ -15,7 +16,11 @@ async function bundleImports(entry: string): Promise { rollupOptions: { external: id => !id.startsWith('.') && !id.startsWith('/') }, }, }) as Rollup.RollupOutput[] - return result[0].output.flatMap(chunk => chunk.type === 'chunk' ? chunk.imports : []) + return result[0].output.filter(chunk => chunk.type === 'chunk') +} + +async function bundleImports(entry: string): Promise { + return (await bundle(entry)).flatMap(chunk => chunk.imports) } describe('markdown-exit/core', () => { @@ -39,3 +44,19 @@ describe('markdown-exit/core', () => { assert.strictEqual(md.render('https://example.com'), '

https://example.com

\n') }) }) + +describe('markdown-exit/parser', () => { + it('bundles neither the renderer nor the presets', async () => { + const modules = (await bundle('../src/index.parser.ts')).flatMap(chunk => chunk.moduleIds) + + assert.include(modules.join('\n'), '/src/parser/parser.ts') + assert.notInclude(modules.join('\n'), '/src/renderer.ts') + assert.notInclude(modules.join('\n'), '/src/presets/') + }) + + it('parses the same tokens as `MarkdownExit`', () => { + const src = '# a\n\n- *b* [c](/d)\n\n| e |\n| - |\n| f |\n\n~~g~~ `h` ©\n' + + assert.deepEqual(new Parser().parse(src), MarkdownExit().parse(src)) + }) +}) diff --git a/packages/markdown-exit/tsdown.config.ts b/packages/markdown-exit/tsdown.config.ts index 8fbf384..77d613d 100644 --- a/packages/markdown-exit/tsdown.config.ts +++ b/packages/markdown-exit/tsdown.config.ts @@ -2,7 +2,7 @@ import { defineConfig } from 'tsdown' export default defineConfig([ { - entry: { index: './src/index.ts', core: './src/index.core.ts' }, + entry: { index: './src/index.ts', core: './src/index.core.ts', parser: './src/index.parser.ts' }, platform: 'neutral', dts: true, },