turnish 1.7.1 → 1.8.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 +56 -35
- package/dist/index.cjs +16 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.iife.js +17 -17
- package/dist/index.iife.js.map +1 -1
- package/dist/index.mjs +108 -102
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +14 -14
- package/dist/index.umd.js.map +1 -1
- package/dist/types/utilities.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
[](https://github.com/mshibanami/turnish/actions/workflows/test.yml)
|
|
4
4
|
[](https://www.npmjs.org/package/turnish)
|
|
5
5
|
|
|
6
|
-
Turnish is a HTML to Markdown converter written in
|
|
6
|
+
Turnish is a HTML to Markdown converter written in TypeScript.
|
|
7
7
|
|
|
8
8
|
This is a fork of [Turndown](https://github.com/mixmark-io/turndown), originally created by Dom Christie.
|
|
9
9
|
|
|
@@ -33,6 +33,10 @@ Turnish has been created to address various issues found in Turndown, while keep
|
|
|
33
33
|
* Modernized build system using Vite
|
|
34
34
|
* Improved code structure for better maintainability
|
|
35
35
|
|
|
36
|
+
> [!WARNING]
|
|
37
|
+
>
|
|
38
|
+
> Same as Turndown, Turnish doesn't aim to sanitize or validate untrusted HTML inputs. Please use Turnish with a dedicated HTML sanitizer if the input HTML is untrusted. Check the [FAQ](./#FAQ) for more details.
|
|
39
|
+
|
|
36
40
|
## Installation
|
|
37
41
|
|
|
38
42
|
npm:
|
|
@@ -87,31 +91,33 @@ Options can be passed in to the constructor on instantiation. For example:
|
|
|
87
91
|
var turnish = new Turnish({ option: 'value' })
|
|
88
92
|
```
|
|
89
93
|
|
|
90
|
-
| Option | Description
|
|
91
|
-
| :--------------------------- |
|
|
92
|
-
| `headingStyle` | Style of headings such as h1, h2...<br/>`setext`: underlined headings<br/>`atx`: hash-prefixed headings
|
|
93
|
-
| `hr` | Any [Thematic break](http://spec.commonmark.org/0.27/#thematic-breaks)
|
|
94
|
-
| `bulletListMarker` | `-`, `+`, or `*`
|
|
95
|
-
| `listMarkerSpaceCount` | Number of spaces after the bullet list marker.<br/><br/>`1` - `4`
|
|
96
|
-
| `listItemIndent` | `tab` or `space`
|
|
97
|
-
| `listItemIndentSpaceCount` | Number of spaces for the nested list item indentation.<br/><br/>`
|
|
98
|
-
| `
|
|
99
|
-
| `
|
|
100
|
-
| `
|
|
101
|
-
| `
|
|
102
|
-
| `
|
|
103
|
-
| `
|
|
104
|
-
| `
|
|
105
|
-
| `
|
|
106
|
-
| `
|
|
94
|
+
| Option | Description | Default |
|
|
95
|
+
| :--------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------- |
|
|
96
|
+
| `headingStyle` | Style of headings such as h1, h2...<br/>`setext`: underlined headings<br/>`atx`: hash-prefixed headings | `atx` |
|
|
97
|
+
| `hr` | Any [Thematic break](http://spec.commonmark.org/0.27/#thematic-breaks) | `---` |
|
|
98
|
+
| `bulletListMarker` | `-`, `+`, or `*` | `-` |
|
|
99
|
+
| `listMarkerSpaceCount` | Number of spaces after the bullet list marker.<br/><br/>`1` - `4` | `1` |
|
|
100
|
+
| `listItemIndent` | `tab` or `space` | `space` |
|
|
101
|
+
| `listItemIndentSpaceCount` | Number of spaces for the nested list item indentation.<br/><br/>`2` or `4` | `4` |
|
|
102
|
+
| `br` | The string used for line breaks. | ` ` |
|
|
103
|
+
| `codeBlockStyle` | `indented` or `fenced` | `fenced` |
|
|
104
|
+
| `fence` | ` ``` ` or `~~~` | ` ``` ` |
|
|
105
|
+
| `emDelimiter` | `_` or `*` | `*` |
|
|
106
|
+
| `strongDelimiter` | `**` or `__` | `**` |
|
|
107
|
+
| `linkStyle` | `inlined` or `referenced` | `inlined` |
|
|
108
|
+
| `linkReferenceStyle` | `full`, `collapsed`, or `shortcut` | `full` |
|
|
109
|
+
| `linkReferenceDeduplication` | How duplicate link reference definitions are handled.<br/><br/>`none`: preserve every duplicate reference as-is.<br/>`full`: merge identical references into one. | `full` |
|
|
110
|
+
| `htmlRetentionMode` | How non-standard HTML fragments are handled.<br/><br/>`standard`: does not preserve non-standard HTML<br/>`preserveAll`: preserve non-standard HTML tags and its children<br/>`markdownIncludingHtml`: preserve non-standard HTML while including Markdown as its child. `markdown="1"` attribute is always added when this mode is enabled. | `standard` |
|
|
111
|
+
| `preformattedCode` | Whether to preserve whitespace and newlines within `<code>` tags, treating them as preformatted content. | `false` |
|
|
107
112
|
|
|
108
113
|
### Advanced Options
|
|
109
114
|
|
|
110
|
-
| Option
|
|
111
|
-
|
|
|
112
|
-
| `blankReplacement`
|
|
113
|
-
| `keepReplacement`
|
|
114
|
-
| `
|
|
115
|
+
| Option | Valid values | Default |
|
|
116
|
+
| :--------------------------------- | :------------------------ | :-------------------------- |
|
|
117
|
+
| `blankReplacement` | rule replacement function | See **Special Rules** below |
|
|
118
|
+
| `keepReplacement` | rule replacement function | See **Special Rules** below |
|
|
119
|
+
| `markdownIncludingHtmlReplacement` | rule replacement function | See **Special Rules** below |
|
|
120
|
+
| `defaultReplacement` | rule replacement function | See **Special Rules** below |
|
|
115
121
|
|
|
116
122
|
## Methods
|
|
117
123
|
|
|
@@ -238,24 +244,23 @@ rules.emphasis = {
|
|
|
238
244
|
|
|
239
245
|
### Special Rules
|
|
240
246
|
|
|
241
|
-
**Blank rule** determines how to handle blank elements. It overrides every rule (even those added via `addRule`). A node is blank if it only contains whitespace, and it's not an `<a>`, `<td>`,`<th>` or a void element. Its behaviour can be customised using the `blankReplacement` option.
|
|
242
|
-
|
|
243
|
-
**
|
|
244
|
-
|
|
245
|
-
**
|
|
246
|
-
|
|
247
|
-
**Default rule** handles nodes which are not recognised by any other rule. By default, it outputs the node's text content (separated by blank lines if it is a block-level element). Its behaviour can be customised with the `defaultReplacement` option.
|
|
247
|
+
* **Blank rule** determines how to handle blank elements. It overrides every rule (even those added via `addRule`). A node is blank if it only contains whitespace, and it's not an `<a>`, `<td>`,`<th>` or a void element. Its behaviour can be customised using the `blankReplacement` option.
|
|
248
|
+
* **Keep rules** determine how to handle the elements that are explicitly marked to be rendered as HTML via the `.keep()` method. By default, no elements are kept. Block-level elements will be separated from surrounding content by blank lines. Its behaviour can be customised using the `keepReplacement` option.
|
|
249
|
+
* **Internal retention rules** determine how to handle elements when `htmlRetentionMode` is not `standard`. It automatically preserves non-standard HTML tags or tags with extra attributes. Its behaviour can be customised using the `keepReplacement` or `markdownIncludingHtmlReplacement` option.
|
|
250
|
+
* **Remove rules** determine which elements to remove altogether. By default, no elements are removed.
|
|
251
|
+
* **Default rule** handles nodes which are not recognised by any other rule. By default, it outputs the node's text content (separated by blank lines if it is a block-level element). Its behaviour can be customised with the `defaultReplacement` option.
|
|
248
252
|
|
|
249
253
|
### Rule Precedence
|
|
250
254
|
|
|
251
255
|
Turnish iterates over the set of rules, and picks the first one that matches the `filter`. The following list describes the order of precedence:
|
|
252
256
|
|
|
253
257
|
1. Blank rule
|
|
254
|
-
2.
|
|
255
|
-
3.
|
|
256
|
-
4.
|
|
257
|
-
5.
|
|
258
|
-
6.
|
|
258
|
+
2. Internal retention rules (if `htmlRetentionMode` is enabled)
|
|
259
|
+
3. Added rules (optional)
|
|
260
|
+
4. Commonmark rules
|
|
261
|
+
5. Keep rules (via `.keep()`)
|
|
262
|
+
6. Remove rules (via `.remove()`)
|
|
263
|
+
7. Default rule
|
|
259
264
|
|
|
260
265
|
## Plugins
|
|
261
266
|
|
|
@@ -273,6 +278,22 @@ If you are confident in doing so, you may want to customise the escaping behavio
|
|
|
273
278
|
|
|
274
279
|
Note: text in code elements is never passed to`escape()`.
|
|
275
280
|
|
|
281
|
+
## FAQ
|
|
282
|
+
|
|
283
|
+
### Does Turnish have full [XSS](https://en.wikipedia.org/wiki/Cross-site_scripting) protection?
|
|
284
|
+
|
|
285
|
+
No. [Like Turndown](https://github.com/mixmark-io/turndown/blob/master/SECURITY.md), Turnish focuses on converting HTML to Markdown and it doesn't aim to sanitize or validate untrusted HTML inputs. We may have some very basic protections against XSS attacks, but they are not comprehensive. Please don't rely on Turnish alone for XSS protection.
|
|
286
|
+
|
|
287
|
+
If you need to handle untrusted HTML, consider using a dedicated HTML sanitizer library before passing the content to Turnish, such as:
|
|
288
|
+
|
|
289
|
+
* [bleach](https://github.com/mozilla/bleach)
|
|
290
|
+
* [DOMPurify](https://github.com/cure53/DOMPurify)
|
|
291
|
+
* [sanitize-html](https://github.com/apostrophecms/sanitize-html)
|
|
292
|
+
* [xss](https://github.com/leizongmin/js-xss)
|
|
293
|
+
* etc.
|
|
294
|
+
|
|
295
|
+
(Sorted alphabetically.)
|
|
296
|
+
|
|
276
297
|
## License
|
|
277
298
|
|
|
278
299
|
[MIT License](LICENSE)
|
package/dist/index.cjs
CHANGED
|
@@ -46,10 +46,10 @@ ${this.indent(1)}`)+this.mapVisit(e.rules,`
|
|
|
46
46
|
`)+this.emit(this.indent(-1))+this.emit(`
|
|
47
47
|
${this.indent()}}`)}declaration(e){return this.compress?this.emit(`${e.property}:${e.value}`,e.position)+this.emit(";"):e.property==="grid-template-areas"?this.emit(this.indent())+this.emit(e.property+": "+e.value.split(`
|
|
48
48
|
`).join(`
|
|
49
|
-
`.padEnd(22)+this.indent()),e.position)+this.emit(";"):this.emit(this.indent())+this.emit(`${e.property}: ${e.value}`,e.position)+this.emit(";")}}const me=(t,e)=>{e=e||{};let n=1,i=1;function r(){const a={line:n,column:i};return o=>(o.position=new le(a,{line:n,column:i},e?.source||""),E(),o)}const s=[];function l(a){const o=new ce(e?.source||"",a,n,i,t);if(!e?.silent)throw o;s.push(o)}function u(){const a=/^{\s*/.exec(t);return!!a&&(m(a),!0)}function c(){const a=/^}/.exec(t);return!!a&&(m(a),!0)}function
|
|
50
|
-
`);i=~y?h.length-y:i+h.length})(o),t=t.slice(o.length),a}function E(){const a=/^\s*/.exec(t);a&&m(a)}function
|
|
51
|
-
`;)e--;return t.substring(0,e)}function q(t){return Y(K(t))}const Ne=["ADDRESS","ARTICLE","ASIDE","AUDIO","BLOCKQUOTE","BODY","CANVAS","CENTER","DD","DIR","DIV","DL","DT","FIELDSET","FIGCAPTION","FIGURE","FOOTER","FORM","FRAMESET","H1","H2","H3","H4","H5","H6","HEADER","HGROUP","HR","HTML","ISINDEX","LI","MAIN","MENU","NAV","NOFRAMES","NOSCRIPT","OL","OUTPUT","P","PRE","SECTION","TABLE","TBODY","TD","TFOOT","TH","THEAD","TR","UL"];function B(t){return P(t,Ne)}const Q=["AREA","BASE","BR","COL","COMMAND","EMBED","HR","IMG","INPUT","KEYGEN","LINK","META","PARAM","SOURCE","TRACK","WBR"];function X(t){return P(t,Q)}function ke(t){return Z(t,Q)}const J=["A","TABLE","THEAD","TBODY","TFOOT","TH","TD","IFRAME","SCRIPT","AUDIO","VIDEO"],Ee=["P","BR","H1","H2","H3","H4","H5","H6","BLOCKQUOTE","UL","OL","LI","PRE","CODE","HR","A","EM","I","STRONG","B","IMG","DIV","SPAN","TABLE","THEAD","TBODY","TR","TH","TD"];function Te(t){return P(t,J)}function xe(t){return Z(t,J)}function P(t,e){return e.indexOf(t.nodeName)>=0}function Z(t,e){return e.some(function(n){return t.nodeType!==x.Element?!1:t.getElementsByTagName(n).length})}function ee(t){return t?t.replace(/(\n+\s*)+/g,`
|
|
52
|
-
`):""}function Re(t){return ee(t).replace(/[\t\r\n]+/g," ").replace(/ {2,}/g," ").trim()}function A(t){return ee(t).replace(/[\t\r\n]+/g," ")}function $e(t){const e=t.parentNode;return e?e.nodeType===x.Element&&e.classList&&e.classList.contains("code")&&e.nodeName==="TD"&&t.nodeName==="PRE":!1}function
|
|
49
|
+
`.padEnd(22)+this.indent()),e.position)+this.emit(";"):this.emit(this.indent())+this.emit(`${e.property}: ${e.value}`,e.position)+this.emit(";")}}const me=(t,e)=>{e=e||{};let n=1,i=1;function r(){const a={line:n,column:i};return o=>(o.position=new le(a,{line:n,column:i},e?.source||""),E(),o)}const s=[];function l(a){const o=new ce(e?.source||"",a,n,i,t);if(!e?.silent)throw o;s.push(o)}function u(){const a=/^{\s*/.exec(t);return!!a&&(m(a),!0)}function c(){const a=/^}/.exec(t);return!!a&&(m(a),!0)}function d(){let a;const o=[];for(E(),g(o);t.length&&t.charAt(0)!=="}"&&(a=oe()||ae(),a);)o.push(a),g(o);return o}function m(a){const o=a[0];return(function(h){const p=h.match(/\n/g);p&&(n+=p.length);const y=h.lastIndexOf(`
|
|
50
|
+
`);i=~y?h.length-y:i+h.length})(o),t=t.slice(o.length),a}function E(){const a=/^\s*/.exec(t);a&&m(a)}function g(a){a=a||[];let o=b();for(;o;)a.push(o),o=b();return a}function b(){const a=r();if(t.charAt(0)!=="/"||t.charAt(1)!=="*")return;const o=/^\/\*[^]*?\*\//.exec(t);return o?(m(o),a({type:f.comment,comment:o[0].slice(2,-2)})):l("End of comment missing")}function H(){const a=/^([^{]+)/.exec(t);if(a)return m(a),((o,h)=>{const p=[];let y=0;for(;y<o.length;){const N=M(o,h,y);if(N===-1)return p.push(o.substring(y)),p;p.push(o.substring(y,N)),y=N+1}return p})(T(a[0]).replace(w,""),[","]).map(o=>T(o))}function R(){const a=r(),o=/^(\*?[-#/*\\\w]+(\[[0-9a-z_-]+\])?)\s*/.exec(t);if(!o)return;m(o);const h=T(o[0]),p=/^:\s*/.exec(t);if(!p)return l("property missing ':'");m(p);let y="";const N=M(t,[";","}"]);N!==-1&&(y=t.substring(0,N),m([y]),y=T(y).replace(w,""));const $=a({type:f.declaration,property:h.replace(w,""),value:y}),_=/^[;\s]*/.exec(t);return _&&m(_),$}function V(){const a=[];if(!u())return l("missing '{'");g(a);let o=R();for(;o;)a.push(o),g(a),o=R();return c()?a:l("missing '}'")}function F(){const a=[],o=r();let h=/^((\d+\.\d+|\.\d+|\d+)%?|[a-z]+)\s*/.exec(t);for(;h;){const p=m(h);a.push(p[1]);const y=/^,\s*/.exec(t);y&&m(y),h=/^((\d+\.\d+|\.\d+|\d+)%?|[a-z]+)\s*/.exec(t)}if(a.length)return o({type:f.keyframe,values:a,declarations:V()||[]})}const ie=C("import"),re=C("charset"),se=C("namespace");function C(a){const o=new RegExp("^@"+a+`\\s*((?::?[^;'"]|"(?:\\\\"|[^"])*?"|'(?:\\\\'|[^'])*?')+)(?:;|$)`);return()=>{const h=r(),p=o.exec(t);if(!p)return;const y=m(p),N={type:a};return N[a]=y[1].trim(),h(N)}}function oe(){if(t[0]==="@")return(function(){const a=r(),o=/^@([-\w]+)?keyframes\s*/.exec(t);if(!o)return;const h=m(o)[1],p=/^([-\w]+)\s*/.exec(t);if(!p)return l("@keyframes missing name");const y=m(p)[1];if(!u())return l("@keyframes missing '{'");let N=g(),$=F();for(;$;)N.push($),N=N.concat(g()),$=F();return c()?a({type:f.keyframes,name:y,vendor:h,keyframes:N}):l("@keyframes missing '}'")})()||(function(){const a=r(),o=/^@media *([^{]+)/.exec(t);if(!o)return;const h=T(m(o)[1]);if(!u())return l("@media missing '{'");const p=g().concat(d());return c()?a({type:f.media,media:h,rules:p}):l("@media missing '}'")})()||(function(){const a=r(),o=/^@custom-media\s+(--\S+)\s+([^{;\s][^{;]*);/.exec(t);if(!o)return;const h=m(o);return a({type:f.customMedia,name:T(h[1]),media:T(h[2])})})()||(function(){const a=r(),o=/^@supports *([^{]+)/.exec(t);if(!o)return;const h=T(m(o)[1]);if(!u())return l("@supports missing '{'");const p=g().concat(d());return c()?a({type:f.supports,supports:h,rules:p}):l("@supports missing '}'")})()||ie()||re()||se()||(function(){const a=r(),o=/^@([-\w]+)?document *([^{]+)/.exec(t);if(!o)return;const h=m(o),p=T(h[1]),y=T(h[2]);if(!u())return l("@document missing '{'");const N=g().concat(d());return c()?a({type:f.document,document:y,vendor:p,rules:N}):l("@document missing '}'")})()||(function(){const a=r(),o=/^@page */.exec(t);if(!o)return;m(o);const h=H()||[];if(!u())return l("@page missing '{'");let p=g(),y=R();for(;y;)p.push(y),p=p.concat(g()),y=R();return c()?a({type:f.page,selectors:h,declarations:p}):l("@page missing '}'")})()||(function(){const a=r(),o=/^@host\s*/.exec(t);if(!o)return;if(m(o),!u())return l("@host missing '{'");const h=g().concat(d());return c()?a({type:f.host,rules:h}):l("@host missing '}'")})()||(function(){const a=r(),o=/^@font-face\s*/.exec(t);if(!o)return;if(m(o),!u())return l("@font-face missing '{'");let h=g(),p=R();for(;p;)h.push(p),h=h.concat(g()),p=R();return c()?a({type:f.fontFace,declarations:h}):l("@font-face missing '}'")})()||(function(){const a=r(),o=/^@container *([^{]+)/.exec(t);if(!o)return;const h=T(m(o)[1]);if(!u())return l("@container missing '{'");const p=g().concat(d());return c()?a({type:f.container,container:h,rules:p}):l("@container missing '}'")})()||(function(){const a=r(),o=/^@starting-style\s*/.exec(t);if(!o)return;if(m(o),!u())return l("@starting-style missing '{'");const h=g().concat(d());return c()?a({type:f.startingStyle,rules:h}):l("@starting-style missing '}'")})()||(function(){const a=r(),o=/^@layer *([^{;@]+)/.exec(t);if(!o)return;const h=T(m(o)[1]);if(!u()){const y=/^[;\s]*/.exec(t);return y&&m(y),a({type:f.layer,layer:h})}const p=g().concat(d());return c()?a({type:f.layer,layer:h,rules:p}):l("@layer missing '}'")})()}function ae(){const a=r(),o=H();return o?(g(),a({type:f.rule,selectors:o,declarations:V()||[]})):l("selector missing")}return v((function(){const a=d();return{type:f.stylesheet,stylesheet:{source:e?.source,rules:a,parsingErrors:s}}})())},he=(t,e)=>new ue(e||{}).compile(t);var fe={parse:me,stringify:he};function pe(t,e){const n=t;return n.isBlock=B(n),n.isCode=n.nodeName==="CODE"||n.parentNode?.isCode,n.isBlank=de(n),n.flankingWhitespace=ge(n,e),n}function de(t){return!X(t)&&!Te(t)&&/^\s*$/i.test(t.textContent||"")&&!ke(t)&&!xe(t)}function ge(t,e){const n=t;if(n.isBlock||e.preformattedCode&&n.isCode)return{leading:"",trailing:""};const i=ye(t.textContent||"");return i.leadingAscii&&j("left",t,e)&&(i.leading=i.leadingNonAscii),i.trailingAscii&&j("right",t,e)&&(i.trailing=i.trailingNonAscii),{leading:i.leading,trailing:i.trailing}}function ye(t){const e=t.match(/^(([ \t\r\n]*)(\s*))(?:(?=\S)[\s\S]*\S)?((\s*?)([ \t\r\n]*))$/);return e?{leading:e[1],leadingAscii:e[2],leadingNonAscii:e[3],trailing:e[4],trailingNonAscii:e[5],trailingAscii:e[6]}:{leading:"",leadingAscii:"",leadingNonAscii:"",trailing:"",trailingNonAscii:"",trailingAscii:""}}function j(t,e,n){let i,r,s;return t==="left"?(i=e.previousSibling,r=/ $/):(i=e.nextSibling,r=/^ /),i&&(i.nodeType===3?s=r.test(i.nodeValue||""):n.preformattedCode&&i.nodeName==="CODE"?s=!1:i.nodeType===1&&!B(i)&&(s=r.test(i.textContent||""))),s||!1}const x={Element:1,Text:3,CDATASection:4,Comment:8};function L(t,e){return Array(e+1).join(t)}function K(t){return t.replace(/^\n*/,"")}function Y(t){let e=t.length;for(;e>0&&t[e-1]===`
|
|
51
|
+
`;)e--;return t.substring(0,e)}function q(t){return Y(K(t))}const Ne=["ADDRESS","ARTICLE","ASIDE","AUDIO","BLOCKQUOTE","BODY","CANVAS","CENTER","DD","DIR","DIV","DL","DT","FIELDSET","FIGCAPTION","FIGURE","FOOTER","FORM","FRAMESET","H1","H2","H3","H4","H5","H6","HEADER","HGROUP","HR","HTML","ISINDEX","LI","MAIN","MENU","NAV","NOFRAMES","NOSCRIPT","OL","OUTPUT","P","PRE","SECTION","TABLE","TBODY","TD","TFOOT","TH","THEAD","TR","UL"];function B(t){return P(t,Ne)}const Q=["AREA","BASE","BR","COL","COMMAND","EMBED","HR","IMG","INPUT","KEYGEN","LINK","META","PARAM","SOURCE","TRACK","WBR"];function X(t){return P(t,Q)}function ke(t){return Z(t,Q)}const J=["A","TABLE","THEAD","TBODY","TFOOT","TH","TD","IFRAME","SCRIPT","AUDIO","VIDEO"],Ee=["P","BR","H1","H2","H3","H4","H5","H6","BLOCKQUOTE","UL","OL","LI","PRE","CODE","HR","A","EM","I","STRONG","B","IMG","DIV","SPAN","TABLE","THEAD","TBODY","TR","TH","TD"];function Te(t){return P(t,J)}function xe(t){return Z(t,J)}function P(t,e){return e.indexOf(t.nodeName)>=0}function Z(t,e){return e.some(function(n){return t.nodeType!==x.Element?!1:t.getElementsByTagName(n).length})}function be(t){return t.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">").replace(/'/g,"'")}function ee(t){return t?t.replace(/(\n+\s*)+/g,`
|
|
52
|
+
`):""}function Re(t){return ee(t).replace(/[\t\r\n]+/g," ").replace(/ {2,}/g," ").trim()}function A(t){return ee(t).replace(/[\t\r\n]+/g," ")}function $e(t){const e=t.parentNode;return e?e.nodeType===x.Element&&e.classList&&e.classList.contains("code")&&e.nodeName==="TD"&&t.nodeName==="PRE":!1}function Ae(t){if(t.nodeName!=="PRE"||t.nodeType!==x.Element)return!1;const e=t.getAttribute("style");if(!e)return!1;const n=fe.parse("pre {"+e+"}");if(!n.stylesheet.rules.length)return!1;const i=n.stylesheet.rules[0];if(!("declarations"in i)||!i.declarations)return!1;const r=i.declarations.find(l=>"property"in l?l.property.toLowerCase()==="font-family":!1);return!r||"value"in r&&!r.value?!1:"value"in r&&r.value.split(",").map(l=>l.trim().toLowerCase()).indexOf("monospace")>=0}function te(t){return $e(t)||Ae(t)?!0:t.nodeName==="PRE"&&t.firstChild&&t.firstChild.nodeName==="CODE"||!1}const k={};k.paragraph={filter:"p",replacement:function(t){return`
|
|
53
53
|
|
|
54
54
|
`+t+`
|
|
55
55
|
|
|
@@ -72,37 +72,37 @@ ${this.indent()}}`)}declaration(e){return this.compress?this.emit(`${e.property}
|
|
|
72
72
|
|
|
73
73
|
`+t+`
|
|
74
74
|
|
|
75
|
-
`}};k.listItem={filter:"li",replacement:function(t,e,n){let i=n.bulletListMarker+" ".repeat(n.listMarkerSpaceCount);const r=e.parentNode;if(r.nodeName==="OL"){const
|
|
76
|
-
`:""),e.childNodes.length>0&&Array.from(e.childNodes).every(
|
|
77
|
-
`:"");let u=0,c=r;for(;c;){if(c.nodeName==="UL"||c.nodeName==="OL"){const
|
|
78
|
-
`+
|
|
75
|
+
`}};k.listItem={filter:"li",replacement:function(t,e,n){let i=n.bulletListMarker+" ".repeat(n.listMarkerSpaceCount);const r=e.parentNode;if(r.nodeName==="OL"){const g=r.getAttribute("start"),b=Array.prototype.indexOf.call(r.children,e);i=(g?Number(g)+b:b+1)+"."+" ".repeat(n.listMarkerSpaceCount)}const s=/\n$/.test(t);if(t=q(t)+(s?`
|
|
76
|
+
`:""),e.childNodes.length>0&&Array.from(e.childNodes).every(g=>g.nodeType===x.Text&&/^\s*$/.test(g.nodeValue||"")||g.nodeType===x.Element&&["UL","OL"].includes(g.nodeName))&&t.trim()!=="")return t+(e.nextSibling?`
|
|
77
|
+
`:"");let u=0,c=r;for(;c;){if(c.nodeName==="UL"||c.nodeName==="OL"){const g=c.parentNode;g&&g.nodeName==="LI"&&u++}c=c.parentNode}let d=n.listItemIndent==="tab"?" ":" ".repeat(n.listItemIndentSpaceCount),m=d.repeat(u);const E=/\n(?!\s*(?:\d+\.\s|[-+*]\s))/gm;return t=t.replace(E,`
|
|
78
|
+
`+d),m+i+t+(e.nextSibling?`
|
|
79
79
|
`:"")}};k.indentedCodeBlock={filter:function(t,e){return!!(e&&e.codeBlockStyle==="indented"&&t.nodeName==="PRE"&&t.firstChild&&t.firstChild.nodeName==="CODE")},replacement:function(t,e){return!e||!e.firstChild?"":`
|
|
80
80
|
|
|
81
81
|
`+e.firstChild.textContent.replace(/\n/g,`
|
|
82
82
|
`)+`
|
|
83
83
|
|
|
84
|
-
`}};k.fencedCodeBlock={filter:function(t,e){return!!(e&&e.codeBlockStyle==="fenced"&&t.nodeName==="PRE")},replacement:function(t,e,n){const i=e,r=i.firstChild,s=r&&r.nodeName==="CODE"?r:i,u=((s.getAttribute("class")||"").match(/(?:lang|language)-(\S+)/)||[null,""])[1],c=s.textContent||"",
|
|
84
|
+
`}};k.fencedCodeBlock={filter:function(t,e){return!!(e&&e.codeBlockStyle==="fenced"&&t.nodeName==="PRE")},replacement:function(t,e,n){const i=e,r=i.firstChild,s=r&&r.nodeName==="CODE"?r:i,u=((s.getAttribute("class")||"").match(/(?:lang|language)-(\S+)/)||[null,""])[1],c=s.textContent||"",d=n.fence?.charAt(0)||"`";let m=3;const E=new RegExp("^"+d+"{3,}","gm");let g;for(;g=E.exec(c);)g[0].length>=m&&(m=g[0].length+1);const b=L(d,m);return`
|
|
85
85
|
|
|
86
|
-
`+
|
|
86
|
+
`+b+u+`
|
|
87
87
|
`+c.replace(/\n$/,"")+`
|
|
88
|
-
`+
|
|
88
|
+
`+b+`
|
|
89
89
|
|
|
90
90
|
`}};k.horizontalRule={filter:"hr",replacement:function(t,e,n){return`
|
|
91
91
|
|
|
92
92
|
`+n.hr+`
|
|
93
93
|
|
|
94
|
-
`}};k.inlineLink={filter:function(t,e){return!!(e?.linkStyle==="inlined"&&t.nodeName==="A"&&t.getAttribute("href"))},replacement:function(t,e){const n=Re(t);let i=e.getAttribute("href")?.replace(/([()])/g,"\\$1"),r;const s=e.getAttribute("title");return s?r=' "'+A(s).replace(/"/g,'\\"')+'"':r="","["+n+"]("+i+r+")"}};const D={filter:function(t,e){return!!(e&&e.linkStyle==="referenced"&&t.nodeName==="A"&&t.getAttribute("href"))},replacement:function(t,e,n){const i=D,r=e.getAttribute("href");let s;const l=e.getAttribute("title");l?s=' "'+A(l)+'"':s="";const u=r+s;let c,
|
|
94
|
+
`}};k.inlineLink={filter:function(t,e){return!!(e?.linkStyle==="inlined"&&t.nodeName==="A"&&t.getAttribute("href"))},replacement:function(t,e){const n=Re(t);let i=e.getAttribute("href")?.replace(/([()])/g,"\\$1"),r;const s=e.getAttribute("title");return s?r=' "'+A(s).replace(/"/g,'\\"')+'"':r="","["+n+"]("+i+r+")"}};const D={filter:function(t,e){return!!(e&&e.linkStyle==="referenced"&&t.nodeName==="A"&&t.getAttribute("href"))},replacement:function(t,e,n){const i=D,r=e.getAttribute("href");let s;const l=e.getAttribute("title");l?s=' "'+A(l)+'"':s="";const u=r+s;let c,d;switch(n.linkReferenceStyle){case"collapsed":c="["+t+"][]",d="["+t+"]: "+u;break;case"shortcut":c="["+t+"]",d="["+t+"]: "+u;break;default:{let m;const E=i.urlReferenceIdMap.get(u);n.linkReferenceDeduplication==="full"&&E?(m=E,d="["+m+"]: "+r+s):(m=i.references.length+1,i.urlReferenceIdMap.set(u,m),d="["+m+"]: "+r+s,i.references.push(d)),c="["+t+"]["+m+"]";break}}return n.linkReferenceStyle!=="full"&&(n.linkReferenceDeduplication==="full"?i.urlReferenceIdMap.has(u)||(i.urlReferenceIdMap.set(u,1),i.references.push(d)):i.references.push(d)),c},references:[],urlReferenceIdMap:new Map,append:()=>{const t=D;let e="";return t.references&&t.references.length&&(e=`
|
|
95
95
|
|
|
96
96
|
`+t.references.join(`
|
|
97
97
|
`)+`
|
|
98
98
|
|
|
99
|
-
`,t.references=[],t.urlReferenceIdMap=new Map),e}};k.referenceLink=D;k.emphasis={filter:["em","i"],replacement:(t,e,n)=>(t=t.trim(),t?n.emDelimiter+t+n.emDelimiter:"")};k.strong={filter:["strong","b"],replacement:(t,e,n)=>(t=t.trim(),t?n.strongDelimiter+t+n.strongDelimiter:"")};k.code={filter:t=>{const e=t.parentNode,n=te(e);return t.nodeName==="CODE"&&!n},replacement:t=>{const e=t.replace(/\r?\n|\r/g," "),n=/^`|^ .*?[^ ].* $|`$/.test(e)?" ":"";let i="`";const r=e.match(/`+/gm)||[];for(;r.includes(i);)i=i+"`";return i+n+e+n+i}};k.image={filter:"img",replacement:function(t,e){const n=e.getAttribute("alt"),i=n?A(n):"",r=e.getAttribute("src")||"",s=e.getAttribute("title"),l=s?A(s):"",u=l?' "'+l+'"':"";return r?"":""}};class ne{options;_keep;_remove;blankRule;keepReplacement;markdownIncludingHtmlReplacement;defaultRule;array;constructor(e){this.options=e,this._keep=[],this._remove=[],this.blankRule={replacement:e.blankReplacement},this.keepReplacement=e.keepReplacement,this.markdownIncludingHtmlReplacement=e.markdownIncludingHtmlReplacement,this.defaultRule={replacement:e.defaultReplacement},this.array=[];for(const n in e.rules)this.array.push(e.rules[n])}add(e,n){this.array.unshift(n)}keep(e){this._keep.unshift({filter:e,replacement:this.keepReplacement})}remove(e){this._remove.unshift({filter:e,replacement:function(){return""}})}forNode(e){if(e.isBlank)return this.blankRule;if(this.options.htmlRetentionMode==="preserveAll"&&this.isUnsupportedElement(e))return{replacement:this.keepReplacement};if(this.options.htmlRetentionMode==="markdownIncludingHtml"&&this.isUnsupportedElement(e))return{replacement:this.markdownIncludingHtmlReplacement};let n;return(n=O(this.array,e,this.options))||(n=O(this._keep,e,this.options))||(n=O(this._remove,e,this.options))?n:this.defaultRule}isUnsupportedElement(e){const n=e.nodeName;if(n==="PRE"&&e.firstChild&&e.firstChild.nodeName==="CODE"){const i=e.firstChild;if(i.attributes&&i.attributes.length>0){for(let r=0;r<i.attributes.length;r++)if(i.attributes[r].name.toLowerCase()!=="class")return!0}}if(e.attributes&&e.attributes.length>0)switch(n){case"IMG":for(let r=0;r<e.attributes.length;r++){const s=e.attributes[r].name.toLowerCase();if(s!=="src"&&s!=="alt"&&s!=="title")return!0}return!1;case"A":for(let r=0;r<e.attributes.length;r++){const s=e.attributes[r].name.toLowerCase();if(s!=="href"&&s!=="title")return!0}return!1;case"CODE":const i=e.parentNode;if(i&&i.nodeName==="PRE"){for(let r=0;r<e.attributes.length;r++)if(e.attributes[r].name.toLowerCase()!=="class")return!0;return!1}default:return!0}return Ee.indexOf(n)===-1}forEach(e){for(let n=0;n<this.array.length;n++)e(this.array[n],n)}}function O(t,e,n){for(let i=0;i<t.length;i++){const r=t[i];if(
|
|
99
|
+
`,t.references=[],t.urlReferenceIdMap=new Map),e}};k.referenceLink=D;k.emphasis={filter:["em","i"],replacement:(t,e,n)=>(t=t.trim(),t?n.emDelimiter+t+n.emDelimiter:"")};k.strong={filter:["strong","b"],replacement:(t,e,n)=>(t=t.trim(),t?n.strongDelimiter+t+n.strongDelimiter:"")};k.code={filter:t=>{const e=t.parentNode,n=te(e);return t.nodeName==="CODE"&&!n},replacement:t=>{const e=t.replace(/\r?\n|\r/g," "),n=/^`|^ .*?[^ ].* $|`$/.test(e)?" ":"";let i="`";const r=e.match(/`+/gm)||[];for(;r.includes(i);)i=i+"`";return i+n+e+n+i}};k.image={filter:"img",replacement:function(t,e){const n=e.getAttribute("alt"),i=n?A(n):"",r=e.getAttribute("src")||"",s=e.getAttribute("title"),l=s?A(s):"",u=l?' "'+l+'"':"";return r?"":""}};class ne{options;_keep;_remove;blankRule;keepReplacement;markdownIncludingHtmlReplacement;defaultRule;array;constructor(e){this.options=e,this._keep=[],this._remove=[],this.blankRule={replacement:e.blankReplacement},this.keepReplacement=e.keepReplacement,this.markdownIncludingHtmlReplacement=e.markdownIncludingHtmlReplacement,this.defaultRule={replacement:e.defaultReplacement},this.array=[];for(const n in e.rules)this.array.push(e.rules[n])}add(e,n){this.array.unshift(n)}keep(e){this._keep.unshift({filter:e,replacement:this.keepReplacement})}remove(e){this._remove.unshift({filter:e,replacement:function(){return""}})}forNode(e){if(e.isBlank)return this.blankRule;if(this.options.htmlRetentionMode==="preserveAll"&&this.isUnsupportedElement(e))return{replacement:this.keepReplacement};if(this.options.htmlRetentionMode==="markdownIncludingHtml"&&this.isUnsupportedElement(e))return{replacement:this.markdownIncludingHtmlReplacement};let n;return(n=O(this.array,e,this.options))||(n=O(this._keep,e,this.options))||(n=O(this._remove,e,this.options))?n:this.defaultRule}isUnsupportedElement(e){const n=e.nodeName;if(n==="PRE"&&e.firstChild&&e.firstChild.nodeName==="CODE"){const i=e.firstChild;if(i.attributes&&i.attributes.length>0){for(let r=0;r<i.attributes.length;r++)if(i.attributes[r].name.toLowerCase()!=="class")return!0}}if(e.attributes&&e.attributes.length>0)switch(n){case"IMG":for(let r=0;r<e.attributes.length;r++){const s=e.attributes[r].name.toLowerCase();if(s!=="src"&&s!=="alt"&&s!=="title")return!0}return!1;case"A":for(let r=0;r<e.attributes.length;r++){const s=e.attributes[r].name.toLowerCase();if(s!=="href"&&s!=="title")return!0}return!1;case"CODE":const i=e.parentNode;if(i&&i.nodeName==="PRE"){for(let r=0;r<e.attributes.length;r++)if(e.attributes[r].name.toLowerCase()!=="class")return!0;return!1}default:return!0}return Ee.indexOf(n)===-1}forEach(e){for(let n=0;n<this.array.length;n++)e(this.array[n],n)}}function O(t,e,n){for(let i=0;i<t.length;i++){const r=t[i];if(Ce(r,e,n))return r}}function Ce(t,e,n){const i=t.filter;if(typeof i=="string"){if(i===e.nodeName.toLowerCase())return!0}else if(Array.isArray(i)){if(i.indexOf(e.nodeName.toLowerCase())>-1)return!0}else if(typeof i=="function"){if(i(e,n))return!0}else throw new TypeError("`filter` needs to be a string, array, or function");return!1}function we(t){const e=t.element,n=t.isBlock,i=t.isVoid,r=t.isPre||function(d){return d.nodeName==="PRE"};if(!e.firstChild||r(e))return;let s=null,l=!1,u=null,c=z(u,e,r);for(;c!==e;){if(c.nodeType===x.Text||c.nodeType===x.CDATASection){const m=c;let E=m.data.replace(/[ \r\n\t]+/g," ");if((!s||/ $/.test(s.data))&&!l&&E[0]===" "&&(E=E.substr(1)),!E){c=S(c);continue}m.data=E,s=m}else if(c.nodeType===x.Element)n(c)||c.nodeName==="BR"?(s&&(s.data=s.data.replace(/ $/,"")),s=null,l=!1):i(c)||r(c)?(s=null,l=!0):s&&(l=!1);else{c=S(c);continue}const d=z(u,c,r);u=c,c=d}s&&(s.data=s.data.replace(/ $/,""),s.data||S(s))}function S(t){const e=t.nextSibling??t.parentNode;return t.parentNode&&t.parentNode.removeChild(t),e}function z(t,e,n){return t&&t.parentNode===e||n(e)?e.nextSibling??e.parentNode:e.firstChild??e.nextSibling??e.parentNode}const I=typeof window<"u"?window:typeof globalThis<"u"?globalThis:{};function Oe(){const t=typeof I.DOMParser<"u"?I.DOMParser:void 0;let e=!1;if(!t)return!1;try{new t().parseFromString("","text/html")&&(e=!0)}catch{}return e}class W{parseFromString(e,n){throw new Error("Not implemented")}}function Se(){if(typeof window<"u"&&typeof document<"u"&&(typeof process>"u"||process.browser),typeof window<"u"){class t extends W{parseFromString(n,i){const r=document.implementation.createHTMLDocument(""),s=document.createElement("template");return s.innerHTML=n,r.body.appendChild(s.content),r}}return new t}else{const t=require("@mixmark-io/domino");class e extends W{parseFromString(i,r){return t.createDocument(i)}}return new e}}const Me=()=>Oe()?new I.DOMParser:Se();function ve(t,{preformattedCode:e}){let n;return typeof t=="string"?n=De().parseFromString('<x-turnish id="turnish-root">'+t+"</x-turnish>","text/html").getElementById("turnish-root"):n=t.cloneNode(!0),we({element:n,isBlock:B,isVoid:X,isPre:e?Ie:void 0}),n}let Le;function De(){return Le??=Me()}function Ie(t){return t.nodeName==="PRE"||t.nodeName==="CODE"}const Be=[[/\\/g,"\\\\"],[/\*/g,"\\*"],[/_/g,"\\_"],[/^-/g,"\\-"],[/^\+ /g,"\\+ "],[/^(=+)/g,"\\$1"],[/^(#{1,6}) /g,"\\$1 "],[/`/g,"\\`"],[/^~~~/g,"\\~~~"],[/\[/g,"\\["],[/\]/g,"\\]"],[/<([^>]*)>/g,"\\<$1\\>"],[/^>/g,"\\>"],[/^(\d+)\. /g,"$1\\. "]],Pe={rules:k,headingStyle:"atx",hr:"---",bulletListMarker:"-",listMarkerSpaceCount:1,listItemIndent:"space",listItemIndentSpaceCount:4,codeBlockStyle:"fenced",fence:"```",emDelimiter:"*",strongDelimiter:"**",linkStyle:"inlined",linkReferenceStyle:"full",linkReferenceDeduplication:"full",br:" ",preformattedCode:!1,htmlRetentionMode:"standard",blankReplacement:(t,e)=>e.isBlock?`
|
|
100
100
|
|
|
101
101
|
`:"",keepReplacement:(t,e)=>e.isBlock?`
|
|
102
102
|
|
|
103
103
|
`+e.outerHTML+`
|
|
104
104
|
|
|
105
|
-
`:e.outerHTML,markdownIncludingHtmlReplacement:(t,e)=>{const n=e.nodeName.toLowerCase();let i="";if(e.attributes&&e.attributes.length>0){const c=[];for(let
|
|
105
|
+
`:e.outerHTML,markdownIncludingHtmlReplacement:(t,e)=>{const n=e.nodeName.toLowerCase();let i="";if(e.attributes&&e.attributes.length>0){const c=[];for(let d=0;d<e.attributes.length;d++){const m=e.attributes[d];c.push(`${m.name}="${be(m.value)}"`)}i=" "+c.join(" ")}i+=' markdown="1"';const r=`<${n}${i}>`,s=`</${n}>`,l=t.trim(),u=r+`
|
|
106
106
|
`+l+`
|
|
107
107
|
`+s;return e.isBlock?`
|
|
108
108
|
|
|
@@ -112,7 +112,7 @@ ${this.indent()}}`)}declaration(e){return this.compress?this.emit(`${e.property}
|
|
|
112
112
|
|
|
113
113
|
`+t+`
|
|
114
114
|
|
|
115
|
-
`:t}};class
|
|
115
|
+
`:t}};class He{options;rules;constructor(e){this.options=Object.assign({},Pe,e),this.rules=new ne(this.options)}render(e){if(!Ve(e))throw new TypeError(e+" is not a string, or an element/document/fragment node.");if(e==="")return"";const n=this.process(ve(e,this.options));return this.postProcess(n)}use(e){if(Array.isArray(e))for(let n=0;n<e.length;n++)this.use(e[n]);else if(typeof e=="function")e(this);else throw new TypeError("plugin must be a Function or an Array of Functions");return this}addRule(e,n){return this.rules.add(e,n),this}keep(e){return this.rules.keep(e),this}remove(e){return this.rules.remove(e),this}escape(e){return Be.reduce(function(n,i){return n.replace(i[0],i[1])},e)}process(e){let n="";const i=e.childNodes;for(let r=0;r<i.length;r++){const s=i[r],l=pe(s,this.options);let u="";if(l.nodeType===x.Text){let c=l.nodeValue??"";if(s.previousSibling&&s.previousSibling.nodeType===x.Element){const d=s.previousSibling;d.nodeName==="INPUT"&&d.getAttribute("type")==="checkbox"&&(c=c.replace(/^\s+/,""))}u=l.isCode?c:this.escape(c)}else l.nodeType===x.Element&&(u=this.replacementForNode(l));n=G(n,u)}return n}postProcess(e){for(const n of this.rules.array)n.append&&(e=G(e,n.append(this.options)));return e.replace(/^[\t\r\n]+/,"").replace(/[\t\r\n\s]+$/,"")}replacementForNode(e){const n=this.rules.forNode(e);let i=this.process(e);const r=e.flankingWhitespace;return(r.leading||r.trailing)&&(i=i.trim()),r.leading+n.replacement(i,e,this.options)+r.trailing}}function G(t,e){const n=Y(t),i=K(e),r=Math.max(t.length-n.length,e.length-i.length),s=`
|
|
116
116
|
|
|
117
|
-
`.substring(0,r);return n+s+i}function Ve(t){return t!=null&&(typeof t=="string"||t.nodeType&&(t.nodeType===1||t.nodeType===9||t.nodeType===11))}exports.NodeTypes=x;exports.Rules=ne;exports.default=
|
|
117
|
+
`.substring(0,r);return n+s+i}function Ve(t){return t!=null&&(typeof t=="string"||t.nodeType&&(t.nodeType===1||t.nodeType===9||t.nodeType===11))}exports.NodeTypes=x;exports.Rules=ne;exports.default=He;exports.isCodeBlock=te;
|
|
118
118
|
//# sourceMappingURL=index.cjs.map
|