@mdzip/renderer 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +164 -3
- package/dist/highlight-core.d.ts +3 -0
- package/dist/highlight-core.d.ts.map +1 -0
- package/dist/highlight-core.js +53 -0
- package/dist/highlight-core.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/markdown.d.ts +209 -0
- package/dist/markdown.d.ts.map +1 -0
- package/dist/markdown.js +521 -0
- package/dist/markdown.js.map +1 -0
- package/dist/page.d.ts +90 -0
- package/dist/page.d.ts.map +1 -0
- package/dist/page.js +277 -0
- package/dist/page.js.map +1 -0
- package/dist/sha256.d.ts +10 -0
- package/dist/sha256.d.ts.map +1 -0
- package/dist/sha256.js +83 -0
- package/dist/sha256.js.map +1 -0
- package/package.json +61 -6
package/README.md
CHANGED
|
@@ -1,3 +1,164 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @mdzip/renderer
|
|
2
|
+
|
|
3
|
+
Renders MDZip Markdown, and `.mdz` archives, to HTML. It's the renderer behind the preview in [MDZip Studio](https://github.com/mdzip-project/mdzip-studio), the [VS Code extension](https://github.com/mdzip-project/mdzip-vscode) and the web editor on [mdzip.org](https://mdzip.org), so a page you build with it looks the way the document looked while it was written.
|
|
4
|
+
|
|
5
|
+
It runs in the browser and in Node.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @mdzip/renderer
|
|
9
|
+
npm install jsdom # only in Node, for sanitizing
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
There are two layers:
|
|
13
|
+
|
|
14
|
+
- **`@mdzip/renderer`**: Markdown to HTML.
|
|
15
|
+
- **`@mdzip/renderer/page`**: an `.mdz` archive to a static page, with its pictures.
|
|
16
|
+
|
|
17
|
+
## Markdown to HTML
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
import { renderMdzipMarkdown } from '@mdzip/renderer';
|
|
21
|
+
|
|
22
|
+
const html = renderMdzipMarkdown('# Hello\n\nSome *Markdown*.');
|
|
23
|
+
// <h1 id="hello">Hello</h1>\n<p>Some <em>Markdown</em>.</p>\n
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
What you get:
|
|
27
|
+
|
|
28
|
+
- GitHub Flavored Markdown: tables, task lists, strikethrough, autolinks.
|
|
29
|
+
- Footnotes (`[^1]`), collected in a section at the end.
|
|
30
|
+
- Code highlighted with highlight.js for common languages (`<code class="hljs language-js">`). Include a highlight.js theme style sheet to color it.
|
|
31
|
+
- ` ```mermaid ` blocks left as `<pre><code class="language-mermaid">`, for Mermaid (or another client-side renderer) to draw.
|
|
32
|
+
- Tables wrapped in `<div class="mdzip-table-scroll">`, so a wide table can scroll on its own.
|
|
33
|
+
- An id on every heading (see below).
|
|
34
|
+
- HTML sanitized with DOMPurify: no scripts, event handlers, `<style>`, inline styles, forms or embeds.
|
|
35
|
+
|
|
36
|
+
### Options
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
renderMdzipMarkdown(markdown, {
|
|
40
|
+
resolveLink: (href) => href.replace(/\.md$/, '.html'), // relative Markdown links only
|
|
41
|
+
externalLinks: 'new-tab', // target="_blank" rel="noopener" on links to other sites
|
|
42
|
+
headingIdPrefix: 'user-content-', // the editor's preview uses this, as GitHub does
|
|
43
|
+
sanitize: false // only for Markdown you trust
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Heading ids
|
|
48
|
+
|
|
49
|
+
Every heading gets an id made from its text, the same way in the editor, on mdzip.org and in the [MDZip Pandoc filter](https://github.com/mdzip-project/mdzip-pandoc), so a link to `#a-heading` works everywhere:
|
|
50
|
+
|
|
51
|
+
- lowercase;
|
|
52
|
+
- punctuation and emoji removed (letters in any language are kept);
|
|
53
|
+
- each run of spaces and hyphens becomes one `-`, with none at either end;
|
|
54
|
+
- a repeated heading gets `-1`, `-2`, … as on GitHub.
|
|
55
|
+
|
|
56
|
+
| Heading | Id |
|
|
57
|
+
|---|---|
|
|
58
|
+
| `## Why isn't it "here"?` | `why-isnt-it-here` |
|
|
59
|
+
| `## mdz — the command line` | `mdz-the-command-line` |
|
|
60
|
+
| `## 🪟 Windows` | `windows` |
|
|
61
|
+
| `## Über Café` | `über-café` |
|
|
62
|
+
| `## Install` (the second one) | `install-1` |
|
|
63
|
+
|
|
64
|
+
`slugifyMdzipHeading(text)` gives you the slug for any text.
|
|
65
|
+
|
|
66
|
+
### In Node
|
|
67
|
+
|
|
68
|
+
Sanitizing needs a DOM. In the browser there's one; in Node, give the renderer a window once:
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
import { JSDOM } from 'jsdom';
|
|
72
|
+
import { useMdzipDomWindow, renderMdzipMarkdown } from '@mdzip/renderer';
|
|
73
|
+
|
|
74
|
+
useMdzipDomWindow(new JSDOM('').window);
|
|
75
|
+
renderMdzipMarkdown(markdown);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
`renderMdzPage()` below does this for you when jsdom is installed. Without a window, `renderMdzipMarkdown()` throws unless you pass `sanitize: false`.
|
|
79
|
+
|
|
80
|
+
### Rendering in parts
|
|
81
|
+
|
|
82
|
+
For a long document you want to render a part at a time (the editor's preview renders the parts near the screen):
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
import { tokenizeMdzipMarkdown, parseMdzipTokens, sanitizeMdzipHtml } from '@mdzip/renderer';
|
|
86
|
+
|
|
87
|
+
const tokens = tokenizeMdzipMarkdown(markdown); // heading ids assigned over the whole document
|
|
88
|
+
const html = sanitizeMdzipHtml(parseMdzipTokens(tokens.slice(0, 20)));
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
HTML from `parseMdzipTokens()` isn't finished until it goes through `sanitizeMdzipHtml()`, or `finishMdzipHtml()` for trusted input: those turn the renderer's markers into task-list checkboxes and heading ids.
|
|
92
|
+
|
|
93
|
+
To match rendered blocks back to their tokens (to line up a source pane with the preview, say), pass `{ markBlocks: true }`. Each top-level token's output is then preceded by a `<!--mdzip-block:N-->` comment, N being its index in the list, which leaves the elements as they were for any HTML transform that runs next. `applyMdzipBlockMarks()` then turns each comment into `data-mdzip-block="N"` on the element after it; allow that attribute when sanitizing (`addAttr: [MDZIP_BLOCK_ATTRIBUTE]`). Each token's `raw` text gives its place in the source. `stripMdzipBlockMarks()` removes the marks from HTML that leaves the page.
|
|
94
|
+
|
|
95
|
+
```js
|
|
96
|
+
const html = sanitizeMdzipHtml(applyMdzipBlockMarks(parseMdzipTokens(tokens, { markBlocks: true })), [{ addAttr: [MDZIP_BLOCK_ATTRIBUTE] }]);
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## An .mdz as a static page
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
import { readFile, writeFile, mkdir } from 'node:fs/promises';
|
|
103
|
+
import { renderMdzPage } from '@mdzip/renderer/page';
|
|
104
|
+
|
|
105
|
+
const page = await renderMdzPage(await readFile('guide.mdz'));
|
|
106
|
+
|
|
107
|
+
await writeFile('index.html', `<!doctype html>
|
|
108
|
+
<meta charset="utf-8">
|
|
109
|
+
<title>${page.title ?? 'Guide'}</title>
|
|
110
|
+
<meta name="description" content="${page.description ?? ''}">
|
|
111
|
+
${page.html}`);
|
|
112
|
+
|
|
113
|
+
for (const asset of page.assets) {
|
|
114
|
+
await mkdir('assets/page-images', { recursive: true });
|
|
115
|
+
await writeFile(asset.path, asset.bytes);
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
(Escape `title` and `description` before putting them in HTML; they come from the archive.)
|
|
120
|
+
|
|
121
|
+
The page is the archive's entry-point document. `page` has:
|
|
122
|
+
|
|
123
|
+
| Field | |
|
|
124
|
+
|---|---|
|
|
125
|
+
| `html` | The document's HTML, sanitized. No `<html>`, `<head>` or style sheet: put it in your own page. |
|
|
126
|
+
| `title`, `description` | From the manifest, or `null`. |
|
|
127
|
+
| `manifest`, `entryPoint` | The manifest, and the archive path of the document. |
|
|
128
|
+
| `assets` | The pictures the document uses: `{ archivePath, fileName, path, url, bytes }`. |
|
|
129
|
+
|
|
130
|
+
### Pictures
|
|
131
|
+
|
|
132
|
+
Each picture the document uses (Markdown images, `<img>`, and the `<source>`s of a `<picture>`), and each video (see below), becomes an asset named by a hash of its bytes, such as `a1b2c3d4e5f6.png`, so the same picture used by several pages is stored once. With no options, its `path` is `assets/page-images/<fileName>`, the HTML points there, and saving each asset at its `path` beside the page is all you need.
|
|
133
|
+
|
|
134
|
+
- **`assetDir`**: another folder, relative to the page.
|
|
135
|
+
- **`assetUrl(fileName, asset)`**: the address to use for pictures stored somewhere else (a CDN, `../assets/…`). `path` stays the same.
|
|
136
|
+
- **`page.withAssetUrls(urls)`**: for hosts that only know a picture's address after uploading it, such as a CMS media library. Render, upload each asset, then get the page with the real addresses. `urls` maps `archivePath` (or `fileName`) to an address, or is a function of the asset.
|
|
137
|
+
|
|
138
|
+
```js
|
|
139
|
+
const urls = {};
|
|
140
|
+
for (const asset of page.assets) urls[asset.archivePath] = await upload(asset.bytes);
|
|
141
|
+
const published = page.withAssetUrls(urls);
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
- **`inlineImages: true`**: the pictures go into the HTML as `data:` URIs, for one self-contained page, and `assets` is empty (except for videos, below). Binary pictures are base64-encoded; SVGs are percent-encoded (`data:image/svg+xml,…`), which is usually smaller. Big pictures make a big page.
|
|
145
|
+
|
|
146
|
+
### Videos
|
|
147
|
+
|
|
148
|
+
An image reference to a video file, `` (`.mp4`, `.m4v`, `.webm`, `.ogv`, `.mov`), renders as `<video class="mdzip-video" controls preload="metadata">` with the alt text as its `aria-label`, and a link to the file inside it for browsers that can't play video at all. Raw-HTML `<video>` tags are kept, without event handlers. On a page, videos (and raw `<video>`/`<source>` `src` and `poster`) are assets like pictures, but are never inlined: with `inlineImages` they're the only assets left. A media fragment stays on the address, so `` starts 83 seconds in. `isMdzipVideoPath(path)` says whether a path is a video, and `parseMdzipMediaTime('#t=1:23')` reads a fragment's start time in seconds (`83`).
|
|
149
|
+
|
|
150
|
+
### SVG diagrams with links
|
|
151
|
+
|
|
152
|
+
An SVG shown with `<img>` is a flat picture: its links can't be clicked. An SVG that contains links (a draw.io diagram saved as `.drawio.svg`, say) is put into the page instead, inside a declarative shadow root (`<template shadowrootmode="open">`), so its styles and ids can't affect the page. Browsers attach it from the HTML alone, without script, and the original `<img>` stays next to it for browsers without declarative shadow DOM, and for crawlers.
|
|
153
|
+
|
|
154
|
+
An SVG with anything script-like (a script, an event handler, a `javascript:` link) or that loads something from outside itself isn't inlined: it stays an `<img>`, which can't run or load anything, and `onWarning` is told why. `inlineLinkedSvg: false` keeps every SVG an `<img>`.
|
|
155
|
+
|
|
156
|
+
A `<picture>` that has such an SVG among its sources becomes one option per image, with a small style sheet that shows the one the `<picture>` would have picked.
|
|
157
|
+
|
|
158
|
+
### Other options
|
|
159
|
+
|
|
160
|
+
`resolveLink`, `externalLinks`, `headingIdPrefix` and `sanitize` work as for `renderMdzipMarkdown()`. `onWarning(message)` reports problems that don't stop the render.
|
|
161
|
+
|
|
162
|
+
## License
|
|
163
|
+
|
|
164
|
+
Apache-2.0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"highlight-core.d.ts","sourceRoot":"","sources":["../src/highlight-core.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,uBAAuB,CAAC;AAqDzC,eAAe,IAAI,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import hljs from 'highlight.js/lib/core';
|
|
2
|
+
import bash from 'highlight.js/lib/languages/bash';
|
|
3
|
+
import cpp from 'highlight.js/lib/languages/cpp';
|
|
4
|
+
import csharp from 'highlight.js/lib/languages/csharp';
|
|
5
|
+
import css from 'highlight.js/lib/languages/css';
|
|
6
|
+
import go from 'highlight.js/lib/languages/go';
|
|
7
|
+
import java from 'highlight.js/lib/languages/java';
|
|
8
|
+
import javascript from 'highlight.js/lib/languages/javascript';
|
|
9
|
+
import json from 'highlight.js/lib/languages/json';
|
|
10
|
+
import markdown from 'highlight.js/lib/languages/markdown';
|
|
11
|
+
import python from 'highlight.js/lib/languages/python';
|
|
12
|
+
import rust from 'highlight.js/lib/languages/rust';
|
|
13
|
+
import sql from 'highlight.js/lib/languages/sql';
|
|
14
|
+
import typescript from 'highlight.js/lib/languages/typescript';
|
|
15
|
+
import xml from 'highlight.js/lib/languages/xml';
|
|
16
|
+
import yaml from 'highlight.js/lib/languages/yaml';
|
|
17
|
+
/**
|
|
18
|
+
* `highlight.js`'s default entry point (`import hljs from 'highlight.js'`)
|
|
19
|
+
* registers every bundled language grammar at import time — a meaningful
|
|
20
|
+
* chunk of the webview bundle (#45). This registers `highlight.js/lib/core`
|
|
21
|
+
* with only the languages `DEFAULT_CODE_BLOCK_LANGUAGES` (view.ts) offers in
|
|
22
|
+
* the code-block-language picker, plus `xml`/`tsx` for fence-info spellings
|
|
23
|
+
* that don't match a toolbar id 1:1. A fenced code block naming a language
|
|
24
|
+
* outside this set renders as plain, unhighlighted code (both call sites
|
|
25
|
+
* already guard with `hljs.getLanguage(language)` before highlighting) —
|
|
26
|
+
* same as today's behavior for a made-up language name, just now also true
|
|
27
|
+
* for real but uncurated languages.
|
|
28
|
+
*/
|
|
29
|
+
hljs.registerLanguage('bash', bash);
|
|
30
|
+
hljs.registerLanguage('cpp', cpp);
|
|
31
|
+
hljs.registerLanguage('csharp', csharp);
|
|
32
|
+
hljs.registerLanguage('css', css);
|
|
33
|
+
hljs.registerLanguage('go', go);
|
|
34
|
+
hljs.registerLanguage('java', java);
|
|
35
|
+
hljs.registerLanguage('javascript', javascript);
|
|
36
|
+
hljs.registerLanguage('json', json);
|
|
37
|
+
hljs.registerLanguage('markdown', markdown);
|
|
38
|
+
hljs.registerLanguage('python', python);
|
|
39
|
+
hljs.registerLanguage('rust', rust);
|
|
40
|
+
hljs.registerLanguage('sql', sql);
|
|
41
|
+
hljs.registerLanguage('typescript', typescript);
|
|
42
|
+
// highlight.js has no distinct tsx grammar; the typescript grammar handles
|
|
43
|
+
// JSX/TSX well enough in practice, and DEFAULT_CODE_BLOCK_LANGUAGES offers
|
|
44
|
+
// 'tsx' as its own toolbar entry (view.ts), so it needs its own registration
|
|
45
|
+
// key rather than relying on 'typescript' aliasing to it.
|
|
46
|
+
hljs.registerLanguage('tsx', typescript);
|
|
47
|
+
// highlight.js's HTML grammar lives under the 'xml' module; register it
|
|
48
|
+
// under both keys since fenced blocks are written as both ```html and ```xml.
|
|
49
|
+
hljs.registerLanguage('html', xml);
|
|
50
|
+
hljs.registerLanguage('xml', xml);
|
|
51
|
+
hljs.registerLanguage('yaml', yaml);
|
|
52
|
+
export default hljs;
|
|
53
|
+
//# sourceMappingURL=highlight-core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"highlight-core.js","sourceRoot":"","sources":["../src/highlight-core.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,uBAAuB,CAAC;AACzC,OAAO,IAAI,MAAM,iCAAiC,CAAC;AACnD,OAAO,GAAG,MAAM,gCAAgC,CAAC;AACjD,OAAO,MAAM,MAAM,mCAAmC,CAAC;AACvD,OAAO,GAAG,MAAM,gCAAgC,CAAC;AACjD,OAAO,EAAE,MAAM,+BAA+B,CAAC;AAC/C,OAAO,IAAI,MAAM,iCAAiC,CAAC;AACnD,OAAO,UAAU,MAAM,uCAAuC,CAAC;AAC/D,OAAO,IAAI,MAAM,iCAAiC,CAAC;AACnD,OAAO,QAAQ,MAAM,qCAAqC,CAAC;AAC3D,OAAO,MAAM,MAAM,mCAAmC,CAAC;AACvD,OAAO,IAAI,MAAM,iCAAiC,CAAC;AACnD,OAAO,GAAG,MAAM,gCAAgC,CAAC;AACjD,OAAO,UAAU,MAAM,uCAAuC,CAAC;AAC/D,OAAO,GAAG,MAAM,gCAAgC,CAAC;AACjD,OAAO,IA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C"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { applyMdzipBlockMarks, assignMdzipHeadingIds, collectMdzipHeadingIds, collectMdzipHeadings, escapeHtml, finishMdzipHtml, hasMdzipDom, highlightMdzipCode, isMdzipVideoPath, MDZIP_BLOCK_ATTRIBUTE, MDZIP_VIDEO_EXTENSIONS, parseMdzipMediaTime, parseMdzipTokens, renderMdzipMarkdown, rewriteMdzipLinks, sanitizeMdzipHtml, slugifyMdzipHeading, stripMdzipBlockMarks, tokenizeMdzipMarkdown, useMdzipDomWindow, walkMdzipTokens } from './markdown.js';
|
|
2
|
+
export type { MdzipFinishOptions, MdzipHeadingInfo, MdzipMarkdownOptions, MdzipParseOptions, MdzipSanitizeContribution, Token, Tokens, TokensList } from './markdown.js';
|
|
3
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,qBAAqB,EACrB,sBAAsB,EACtB,oBAAoB,EACpB,UAAU,EACV,eAAe,EACf,WAAW,EACX,kBAAkB,EAClB,gBAAgB,EAChB,qBAAqB,EACrB,sBAAsB,EACtB,mBAAmB,EACnB,gBAAgB,EAChB,mBAAmB,EACnB,iBAAiB,EACjB,iBAAiB,EACjB,mBAAmB,EACnB,oBAAoB,EACpB,qBAAqB,EACrB,iBAAiB,EACjB,eAAe,EAChB,MAAM,eAAe,CAAC;AACvB,YAAY,EACV,kBAAkB,EAClB,gBAAgB,EAChB,oBAAoB,EACpB,iBAAiB,EACjB,yBAAyB,EACzB,KAAK,EACL,MAAM,EACN,UAAU,EACX,MAAM,eAAe,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { applyMdzipBlockMarks, assignMdzipHeadingIds, collectMdzipHeadingIds, collectMdzipHeadings, escapeHtml, finishMdzipHtml, hasMdzipDom, highlightMdzipCode, isMdzipVideoPath, MDZIP_BLOCK_ATTRIBUTE, MDZIP_VIDEO_EXTENSIONS, parseMdzipMediaTime, parseMdzipTokens, renderMdzipMarkdown, rewriteMdzipLinks, sanitizeMdzipHtml, slugifyMdzipHeading, stripMdzipBlockMarks, tokenizeMdzipMarkdown, useMdzipDomWindow, walkMdzipTokens } from './markdown.js';
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,qBAAqB,EACrB,sBAAsB,EACtB,oBAAoB,EACpB,UAAU,EACV,eAAe,EACf,WAAW,EACX,kBAAkB,EAClB,gBAAgB,EAChB,qBAAqB,EACrB,sBAAsB,EACtB,mBAAmB,EACnB,gBAAgB,EAChB,mBAAmB,EACnB,iBAAiB,EACjB,iBAAiB,EACjB,mBAAmB,EACnB,oBAAoB,EACpB,qBAAqB,EACrB,iBAAiB,EACjB,eAAe,EAChB,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { type Token, type Tokens, type TokensList } from 'marked';
|
|
2
|
+
export type { Token, Tokens, TokensList };
|
|
3
|
+
/**
|
|
4
|
+
* Gives the sanitizer a DOM window to work in, for environments without a
|
|
5
|
+
* global one (Node). The window isn't assigned to any global.
|
|
6
|
+
*
|
|
7
|
+
* ```js
|
|
8
|
+
* import { JSDOM } from 'jsdom';
|
|
9
|
+
* useMdzipDomWindow(new JSDOM('').window);
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare function useMdzipDomWindow(window: unknown): void;
|
|
13
|
+
/** Whether sanitizing can run here: a browser, or a window from {@link useMdzipDomWindow} or `globalThis.window`. */
|
|
14
|
+
export declare function hasMdzipDom(): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Narrow, opt-in relaxations a render extension contributes to the single
|
|
17
|
+
* DOMPurify pass so its output survives sanitization.
|
|
18
|
+
*
|
|
19
|
+
* The default policy is HTML-only and strips SVG, inline styles, `<style>`,
|
|
20
|
+
* and data attributes. An extension that injects richer markup — inline SVG
|
|
21
|
+
* for a diagram, say — declares exactly what it needs here. Keep contributions
|
|
22
|
+
* as narrow as possible: they widen the policy for the **whole** render pass
|
|
23
|
+
* (the extension output and the surrounding markdown HTML are sanitized
|
|
24
|
+
* together), so anything allowed here is allowed everywhere in that render.
|
|
25
|
+
*/
|
|
26
|
+
export interface MdzipSanitizeContribution {
|
|
27
|
+
/**
|
|
28
|
+
* Enable DOMPurify's `svg` + `svgFilters` profiles so inline SVG survives.
|
|
29
|
+
* Even with this on, DOMPurify still strips scripts and event handlers from
|
|
30
|
+
* the SVG — extensions should additionally sanitize and lock down their own
|
|
31
|
+
* SVG before injecting it.
|
|
32
|
+
*/
|
|
33
|
+
allowSvg?: boolean;
|
|
34
|
+
/** Extra element names to allow beyond the default policy. */
|
|
35
|
+
addTags?: readonly string[];
|
|
36
|
+
/** Extra attribute names to allow beyond the default policy. */
|
|
37
|
+
addAttr?: readonly string[];
|
|
38
|
+
/** Tags to drop from the default forbid-list (e.g. re-allow `style`). */
|
|
39
|
+
unforbidTags?: readonly string[];
|
|
40
|
+
/** Attributes to drop from the default forbid-list (e.g. re-allow inline `style`). */
|
|
41
|
+
unforbidAttr?: readonly string[];
|
|
42
|
+
}
|
|
43
|
+
/** Options for the last step of a render, after sanitizing (or instead of it). */
|
|
44
|
+
export interface MdzipFinishOptions {
|
|
45
|
+
/**
|
|
46
|
+
* Put in front of every heading id. Default none (`<h2 id="intro">`). The
|
|
47
|
+
* MDZip editor's preview uses `user-content-`, as GitHub does.
|
|
48
|
+
*/
|
|
49
|
+
headingIdPrefix?: string;
|
|
50
|
+
/**
|
|
51
|
+
* `'new-tab'` adds `target="_blank" rel="noopener"` to links to other sites
|
|
52
|
+
* (`http:`, `https:` and `//`). Default `'same-tab'`: links are left as they are.
|
|
53
|
+
*/
|
|
54
|
+
externalLinks?: 'same-tab' | 'new-tab';
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Sanitizes rendered HTML with the MDZip DOMPurify policy, then finishes it
|
|
58
|
+
* (see {@link finishMdzipHtml}).
|
|
59
|
+
*
|
|
60
|
+
* The policy strips data attributes and inline styles. Markdown render
|
|
61
|
+
* extensions that need to find placeholders again later should use class or
|
|
62
|
+
* id markers and carry payloads out-of-band (a side map keyed by marker id),
|
|
63
|
+
* not element attributes.
|
|
64
|
+
*
|
|
65
|
+
* Pass `contributions` (an extension's {@link MdzipSanitizeContribution}s) to
|
|
66
|
+
* widen the policy just enough for richer output such as inline SVG.
|
|
67
|
+
*/
|
|
68
|
+
export declare function sanitizeMdzipHtml(html: string, contributions?: readonly MdzipSanitizeContribution[], options?: MdzipFinishOptions): string;
|
|
69
|
+
/**
|
|
70
|
+
* Turns the renderer's markers into their final markup: task-list checkboxes
|
|
71
|
+
* and heading ids. {@link sanitizeMdzipHtml} calls this; call it yourself only
|
|
72
|
+
* for HTML from {@link parseMdzipTokens} that isn't sanitized.
|
|
73
|
+
*
|
|
74
|
+
* The markers exist because the sanitizer would otherwise remove what they
|
|
75
|
+
* stand for: `<input>` is forbidden (so raw-HTML inputs are always stripped,
|
|
76
|
+
* and only task items get checkboxes), and DOMPurify drops an id that names a
|
|
77
|
+
* `document` property, such as a heading called "Images" or "Links".
|
|
78
|
+
*/
|
|
79
|
+
export declare function finishMdzipHtml(html: string, options?: MdzipFinishOptions): string;
|
|
80
|
+
/**
|
|
81
|
+
* The heading slug shared by the MDZip editor, mdzip.org and the MDZip Pandoc
|
|
82
|
+
* filter: lowercased; everything but letters, marks, numbers, `_`, `-` and
|
|
83
|
+
* whitespace removed (so punctuation and emoji go); each run of whitespace and
|
|
84
|
+
* hyphens turned into one `-`; and no `-` at either end.
|
|
85
|
+
*
|
|
86
|
+
* `Why isn't it "here"?` → `why-isnt-it-here`, `mdz — the CLI` → `mdz-the-cli`,
|
|
87
|
+
* `🪟 Windows` → `windows`, `Über Café` → `über-café`.
|
|
88
|
+
*/
|
|
89
|
+
export declare function slugifyMdzipHeading(text: string): string;
|
|
90
|
+
/**
|
|
91
|
+
* Gives every heading token (including ones nested in blockquotes and lists)
|
|
92
|
+
* the id its rendered `<hN>` will carry, in document order, numbering repeats
|
|
93
|
+
* as GitHub does (`intro`, `intro-1`, `intro-2`). Has to run over the whole
|
|
94
|
+
* document's tokens before they are split into chunks — a chunk rendered on
|
|
95
|
+
* its own can't know how many earlier chunks already used a slug. A heading
|
|
96
|
+
* whose text slugifies to nothing (e.g. only punctuation) gets no id.
|
|
97
|
+
*/
|
|
98
|
+
export declare function assignMdzipHeadingIds(tokens: readonly Token[]): void;
|
|
99
|
+
/** The ids {@link assignMdzipHeadingIds} gave the headings in `tokens`, in order (without any prefix). */
|
|
100
|
+
export declare function collectMdzipHeadingIds(tokens: readonly Token[]): string[];
|
|
101
|
+
/** A heading as {@link collectMdzipHeadings} lists it. */
|
|
102
|
+
export interface MdzipHeadingInfo {
|
|
103
|
+
/** The id its `<hN>` carries (without any prefix). */
|
|
104
|
+
id: string;
|
|
105
|
+
/** Its text, without Markdown syntax or extra whitespace. */
|
|
106
|
+
text: string;
|
|
107
|
+
/** 1 to 6. */
|
|
108
|
+
level: number;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Every heading that has an id, in document order, for a table of contents.
|
|
112
|
+
* Run {@link assignMdzipHeadingIds} (or {@link tokenizeMdzipMarkdown}) on the
|
|
113
|
+
* tokens first.
|
|
114
|
+
*/
|
|
115
|
+
export declare function collectMdzipHeadings(tokens: readonly Token[]): MdzipHeadingInfo[];
|
|
116
|
+
/**
|
|
117
|
+
* Highlights `code` with highlight.js when `language` is one of the bundled
|
|
118
|
+
* grammars (`vue` counts as `html`); returns `null` otherwise. The output is
|
|
119
|
+
* highlight.js's span markup, for a `<code class="hljs">`.
|
|
120
|
+
*/
|
|
121
|
+
export declare function highlightMdzipCode(code: string, language: string): string | null;
|
|
122
|
+
/**
|
|
123
|
+
* File extensions an image reference (``) renders as a video
|
|
124
|
+
* player instead of an `<img>`. Whether a given file plays depends on the
|
|
125
|
+
* browser: most play H.264 or VP9 video in an `.mp4` or `.webm`.
|
|
126
|
+
*/
|
|
127
|
+
export declare const MDZIP_VIDEO_EXTENSIONS: readonly string[];
|
|
128
|
+
/** Whether a path or URL names a video file, ignoring any `?query` or `#fragment` (`reel.mp4#t=83` is a video). */
|
|
129
|
+
export declare function isMdzipVideoPath(path: string): boolean;
|
|
130
|
+
/**
|
|
131
|
+
* The start time, in seconds, of a media fragment (`#t=83`, `#t=1:23`,
|
|
132
|
+
* `#t=npt:0:01:23.5`, `#t=83,95`), or `null` when the fragment has none.
|
|
133
|
+
* Accepts the fragment with or without its `#`, or a whole URL.
|
|
134
|
+
*/
|
|
135
|
+
export declare function parseMdzipMediaTime(fragmentOrUrl: string): number | null;
|
|
136
|
+
/**
|
|
137
|
+
* Calls `callback` on every token, depth first, in the same order as marked's
|
|
138
|
+
* own `walkTokens` (table cells, list items, extension child tokens, then
|
|
139
|
+
* `tokens`). Marked's version also collects every callback's return value by
|
|
140
|
+
* concatenating arrays as it goes, which is quadratic in the token count: on
|
|
141
|
+
* a large chat export it took 13 seconds plus 6 of garbage collection. Those
|
|
142
|
+
* values only matter to marked's async mode, which isn't used here.
|
|
143
|
+
*/
|
|
144
|
+
export declare function walkMdzipTokens(tokens: Token[], callback: (token: Token) => unknown): void;
|
|
145
|
+
/**
|
|
146
|
+
* Lexes Markdown into top-level block tokens with heading ids assigned, for
|
|
147
|
+
* callers that render it in parts (see {@link parseMdzipTokens}). The tokens
|
|
148
|
+
* tile the source: their `raw` strings joined give back the whole input.
|
|
149
|
+
*/
|
|
150
|
+
export declare function tokenizeMdzipMarkdown(markdown: string): TokensList;
|
|
151
|
+
/**
|
|
152
|
+
* Renders tokens from {@link tokenizeMdzipMarkdown} to HTML that is neither
|
|
153
|
+
* sanitized nor finished: pass it through {@link sanitizeMdzipHtml} (or,
|
|
154
|
+
* for trusted input, {@link finishMdzipHtml}) before use.
|
|
155
|
+
*/
|
|
156
|
+
export declare function parseMdzipTokens(tokens: readonly Token[], options?: MdzipParseOptions): string;
|
|
157
|
+
/**
|
|
158
|
+
* The attribute {@link applyMdzipBlockMarks} puts on the first element each
|
|
159
|
+
* top-level token rendered to: the token's index in the list given to
|
|
160
|
+
* {@link parseMdzipTokens}. Allow it when sanitizing (`addAttr`) to keep it.
|
|
161
|
+
*/
|
|
162
|
+
export declare const MDZIP_BLOCK_ATTRIBUTE = "data-mdzip-block";
|
|
163
|
+
export interface MdzipParseOptions {
|
|
164
|
+
/**
|
|
165
|
+
* Put a `<!--mdzip-block:<index>-->` comment before each top-level token's
|
|
166
|
+
* output, so the rendered blocks can be matched back to their tokens, and
|
|
167
|
+
* through the tokens' `raw` text to source lines (the editor's split-view
|
|
168
|
+
* scroll sync). The marks are comments so that code transforming the HTML
|
|
169
|
+
* next sees the elements as they'd be without them; then
|
|
170
|
+
* {@link applyMdzipBlockMarks} turns each into a `data-mdzip-block`
|
|
171
|
+
* attribute on the element after it.
|
|
172
|
+
*/
|
|
173
|
+
markBlocks?: boolean;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Turns the comments {@link parseMdzipTokens} `markBlocks` leaves into
|
|
177
|
+
* `data-mdzip-block="<index>"` on the element that follows each, and drops
|
|
178
|
+
* those with no element after them (a blank line, a raw HTML comment).
|
|
179
|
+
*/
|
|
180
|
+
export declare function applyMdzipBlockMarks(html: string): string;
|
|
181
|
+
/** Removes block marks, comments or attributes, from HTML that leaves the page (a copy, an export). */
|
|
182
|
+
export declare function stripMdzipBlockMarks(html: string): string;
|
|
183
|
+
/**
|
|
184
|
+
* Rewrites the `href` of every Markdown link in `tokens` through `resolve`.
|
|
185
|
+
* Links that are absolute (`https:`, `mailto:`, any scheme), protocol-relative
|
|
186
|
+
* or in-page (`#…`) are left alone. Raw HTML `<a>` tags aren't touched.
|
|
187
|
+
*/
|
|
188
|
+
export declare function rewriteMdzipLinks(tokens: Token[], resolve: (href: string) => string): void;
|
|
189
|
+
export interface MdzipMarkdownOptions extends MdzipFinishOptions {
|
|
190
|
+
/**
|
|
191
|
+
* Sanitize with the MDZip DOMPurify policy. Default `true`. Turn it off only
|
|
192
|
+
* for Markdown you trust: raw HTML in it reaches the output unchanged.
|
|
193
|
+
* In Node, sanitizing needs a DOM window; see {@link useMdzipDomWindow}.
|
|
194
|
+
*/
|
|
195
|
+
sanitize?: boolean;
|
|
196
|
+
/** Policy relaxations for sanitizing; see {@link MdzipSanitizeContribution}. */
|
|
197
|
+
sanitizeContributions?: readonly MdzipSanitizeContribution[];
|
|
198
|
+
/** Rewrites relative Markdown link targets; see {@link rewriteMdzipLinks}. */
|
|
199
|
+
resolveLink?: (href: string) => string;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* MDZip Markdown to HTML: GitHub Flavored Markdown with footnotes, task
|
|
203
|
+
* lists, highlighted code, heading ids, and tables in a scrolling wrapper;
|
|
204
|
+
* ` ```mermaid ` blocks stay as `<pre><code class="language-mermaid">` for a
|
|
205
|
+
* client-side renderer; sanitized unless `sanitize: false`.
|
|
206
|
+
*/
|
|
207
|
+
export declare function renderMdzipMarkdown(markdown: string, options?: MdzipMarkdownOptions): string;
|
|
208
|
+
export declare function escapeHtml(value: string): string;
|
|
209
|
+
//# sourceMappingURL=markdown.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../src/markdown.ts"],"names":[],"mappings":"AACA,OAAO,EAAU,KAAK,KAAK,EAAE,KAAK,MAAM,EAAE,KAAK,UAAU,EAAE,MAAM,QAAQ,CAAC;AAI1E,YAAY,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC;AAoC1C;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,OAAO,GA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