-
2025-03-17
-
**turndown**Public
-
mixmark-io/turndown
t
| Name |
|
| | ---- | ---- | ---- | |
#+BEGIN_EXPORT hiccup [:a {:href “https://github.com/martincizek”} [:img {:alt “martincizek”, :width “20”, :src “https://avatars.githubusercontent.com/u/9081731?v=4&size=40”, :aria-label “martincizek”, :height “20”}]] #+END_EXPORTmartincizek
cc73387 · 10 months ago
| |
|
|
11 months ago
| |
|
|
11 months ago
| |
|
Escape inline link attributes. Fix #459.
|
11 months ago
| |
|
Escape inline link attributes. Fix #459.
|
11 months ago
| |
|
|
8 years ago
| |
|
|
8 years ago
| |
|
|
8 years ago
| |
|
Use to GitHub Actions instead of Travis CI
|
11 months ago
| |
|
|
11 months ago
| |
|
Update links similarly to #389.
|
4 years ago
| |
|
|
10 months ago
| |
|
|
10 months ago
|
-
Repository files navigation
-
Turndown
Convert HTML into Markdown with JavaScript.
-
Project Updates
-
to-markdownhas been renamed to Turndown. See the migration guide for details. -
Turndown repository has changed its URL to https://github.com/mixmark-io/turndown.
-
Installation
npm:
npm install turndownBrowser:
\<script src="https://unpkg.com/turndown/dist/turndown.js"\>\</script\>For usage with RequireJS, UMD versions are located in
lib/turndown.umd.js(for Node.js) andlib/turndown.browser.umd.jsfor browser usage. These files are generated when the npm package is published. To generate them manually, clone this repo and runnpm run build. -
Usage
// For Node.js var TurndownService = require('turndown') var turndownService = new TurndownService() var markdown = turndownService.turndown('\<h1\>Hello world!\</h1\>')Turndown also accepts DOM nodes as input (either element nodes, document nodes, or document fragment nodes):
var markdown = turndownService.turndown(document.getElementById('content')) -
Options
Options can be passed in to the constructor on instantiation. For example:
var turndownService = new TurndownService({ option: 'value' })Option Valid values Default headingStylesetextoratxsetexthrAny Thematic break * * *bulletListMarker-,+, or**codeBlockStyleindentedorfencedindentedfence````` or ~~~````` emDelimiter_or*_strongDelimiter**or__**linkStyleinlinedorreferencedinlinedlinkReferenceStylefull,collapsed, orshortcutfullpreformattedCodefalseortruefalse -
Advanced Options
Option Valid values Default blankReplacementrule replacement function See Special Rules below keepReplacementrule replacement function See Special Rules below defaultReplacementrule replacement function See Special Rules below -
Methods
-
addRule(key, rule)The
keyparameter is a unique name for the rule for easy reference. Example:turndownService.addRule('strikethrough', { filter: ['del', 's', 'strike'], replacement: function (content) { return '~' + content + '~' } })addRulereturns theTurndownServiceinstance for chaining.See Extending with Rules below.
-
keep(filter)Determines which elements are to be kept and rendered as HTML. By default, Turndown does not keep any elements. The filter parameter works like a rule filter (see section on filters belows). Example:
turndownService.keep(['del', 'ins']) turndownService.turndown('\<p\>Hello \<del\>world\</del\>\<ins\>World\</ins\>\</p\>') // 'Hello \<del\>world\</del\>\<ins\>World\</ins\>'This will render
\<del\>and\<ins\>elements as HTML when converted.keepcan be called multiple times, with the newly added keep filters taking precedence over older ones. Keep filters will be overridden by the standard CommonMark rules and any added rules. To keep elements that are normally handled by those rules, add a rule with the desired behaviour.keepreturns theTurndownServiceinstance for chaining. -
remove(filter)Determines which elements are to be removed altogether i.e. converted to an empty string. By default, Turndown does not remove any elements. The filter parameter works like a rule filter (see section on filters belows). Example:
turndownService.remove('del') turndownService.turndown('\<p\>Hello \<del\>world\</del\>\<ins\>World\</ins\>\</p\>') // 'Hello World'This will remove
\<del\>elements (and contents).removecan be called multiple times, with the newly added remove filters taking precedence over older ones. Remove filters will be overridden by the keep filters, standard CommonMark rules, and any added rules. To remove elements that are normally handled by those rules, add a rule with the desired behaviour.removereturns theTurndownServiceinstance for chaining. -
use(plugin|array)Use a plugin, or an array of plugins. Example:
// Import plugins from turndown-plugin-gfm var turndownPluginGfm = require('turndown-plugin-gfm') var gfm = turndownPluginGfm.gfm var tables = turndownPluginGfm.tables var strikethrough = turndownPluginGfm.strikethrough // Use the gfm plugin turndownService.use(gfm) // Use the table and strikethrough plugins only turndownService.use([tables, strikethrough])usereturns theTurndownServiceinstance for chaining.See Plugins below.
-
Extending with Rules
Turndown can be extended by adding rules. A rule is a plain JavaScript object with
filterandreplacementproperties. For example, the rule for converting\<p\>elements is as follows:{ filter: 'p', replacement: function (content) { return '\n\n' + content + '\n\n' } }The filter selects
\<p\>elements, and the replacement function returns the\<p\>contents separated by two new lines. -
filterString|Array|FunctionThe filter property determines whether or not an element should be replaced with the rule’s
replacement. DOM nodes can be selected simply using a tag name or an array of tag names: -
filter: 'p'will select\<p\>elements -
filter: ['em', 'i']will select\<em\>or\<i\>elementsThe tag names in the
filterproperty are expected in lowercase, regardless of their form in the document.Alternatively, the filter can be a function that returns a boolean depending on whether a given node should be replaced. The function is passed a DOM node as well as the
TurndownServiceoptions. For example, the following rule selects\<a\>elements (with anhref) when thelinkStyleoption isinlined:filter: function (node, options) { return ( options.linkStyle === 'inlined' && node.nodeName === 'A' && node.getAttribute('href') ) } -
replacementFunctionThe replacement function determines how an element should be converted. It should return the Markdown string for a given node. The function is passed the node’s content, the node itself, and the
TurndownServiceoptions.The following rule shows how
\<em\>elements are converted:rules.emphasis = { filter: ['em', 'i'], replacement: function (content, node, options) { return options.emDelimiter + content + options.emDelimiter } } -
Special Rules
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 theblankReplacementoption.Keep rules determine how to handle the elements that should not be converted, i.e. rendered as HTML in the Markdown output. 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
keepReplacementoption.Remove rules determine which elements to remove altogether. By default, no elements are removed.
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
defaultReplacementoption. -
Rule Precedence
Turndown iterates over the set of rules, and picks the first one that matches the
filter. The following list describes the order of precedence: -
Blank rule
-
Added rules (optional)
-
Commonmark rules
-
Keep rules
-
Remove rules
-
Default rule
-
Plugins
The plugin API provides a convenient way for developers to apply multiple extensions. A plugin is just a function that is called with the
TurndownServiceinstance. -
Escaping Markdown Characters
Turndown uses backslashes (
\) to escape Markdown characters in the HTML input. This ensures that these characters are not interpreted as Markdown when the output is compiled back to HTML. For example, the contents of\<h1\>1. Hello world\</h1\>needs to be escaped to1\. Hello world, otherwise it will be interpreted as a list item rather than a heading.To avoid the complexity and the performance implications of parsing the content of every HTML element as Markdown, Turndown uses a group of regular expressions to escape potential Markdown syntax. As a result, the escaping rules can be quite aggressive.
-
Overriding
TurndownService.prototype.escapeIf you are confident in doing so, you may want to customise the escaping behaviour to suit your needs. This can be done by overriding
TurndownService.prototype.escape.escapetakes the text of each HTML element and should return a version with the Markdown characters escaped.Note: text in code elements is never passed to
escape. -
License
turndown is copyright © 2017+ Dom Christie and released under the MIT license.
-
-
teset
-
遗憾的时,通过 ToolbarItem 设置输入辅助视图目前还有以下不足:
-
显示内容受限
高度固定,且无法利用辅助视图的完整显示区域。同其他类型的 Toolbar 类似,SwiftUI 会干预内容的排版。
-
无法对同一视图中多个 TextField 分别设定辅助视图
在 ToolbarItem 中无法使用稍微复杂一点的判断语法。如果分别对不同的 TextField 进行设定,SwiftUI 会将所有的内容合并起来显示。
>
目前 SwiftUI 对 toolbar 内容的干预和处理有些过头。初衷是好的,帮助开发者更轻松的组织按钮且自动针对不同平台优化并最佳显示效果。但 toolbar 及 ToolbarItem 的 ResultBuilder 的限制太多,无法在其中进行更复杂的逻辑判断。将键盘辅助视图集成到 toolbar 的逻辑中也有些令人令人费解。
-
通过 UIKit 创建
当前阶段,通过 UIKit 来创建键盘辅助视图仍是 SwiftUI 下的最优方案。不仅可以获得完全的视图显示控制能力,并且可以对同一视图下的多个 TextField 进行分别设置。
Swift
extension* UIView* { func constrainEdges(to* other*: UIView) { translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ leadingAnchor.constraint(equalTo: other.leadingAnchor), trailingAnchor.constraint(equalTo: other.trailingAnchor), topAnchor.constraint(equalTo: other.topAnchor), bottomAnchor.constraint(equalTo: other.bottomAnchor), ]) } } extension* View* { func inputAccessoryView\<*Content*:* View*\>(@ViewBuilder* content*: @escaping () -\> Content) -\> some View { introspectTextField { td in let viewController = UIHostingController(rootView: content()) viewController.view.constrainEdges(to: viewController.view) td.inputAccessoryView = viewController.view } } func inputAccessoryView\<*Content*:* View*\>(*content*: Content) -\> some View { introspectTextField { td in let viewController = UIHostingController(rootView: content) viewController.view.constrainEdges(to: viewController.view) td.inputAccessoryView = viewController.view } } }调用:
-
-
ttt‘
-
See Plugins below.
-
Extending with Rules
Turndown can be extended by adding rules. A rule is a plain JavaScript object with
filterandreplacementproperties. For example, the rule for converting\<p\>elements is as follows:{ filter: 'p', replacement: function (content) { return 'nn' + content + 'nn' } }The filter selects
\<p\>elements, and the replacement function returns the\<p\>contents separated by two new lines. -
-
filterString|Array|FunctionThe filter property determines whether or not an element should be replaced with the rule’s
replacement. DOM nodes can be selected simply using a tag name or an array of tag names: -
filter: 'p'will select\<p\>elements -
filter: ['em', 'i']will select\<em\>or\<i\>elements The tag names in thefilterproperty are expected in lowercase, regardless of their form in the document.Alternatively, the filter can be a function that returns a boolean depending on whether a given node should be replaced. The function is passed a DOM node as well as the
TurndownServiceoptions. For example, the following rule selects\<a\>elements (with anhref) when thelinkStyleoption isinlined:filter: function (node, options) { return ( options.linkStyle === 'inlined' && node.nodeName === 'A' && node.getAttribute('href') ) } -
-
replacementFunctionThe replacement function determines how an element should be converted. It should return the Markdown string for a given node. The function is passed the node’s content, the node itself, and the
TurndownServiceoptions.The following rule shows how
\<em\>elements are converted:rules.emphasis = { filter: ['em', 'i'], replacement: function (content, node, options) { return options.emDelimiter + content + options.emDelimiter } } -
-
Special Rules
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 theblankReplacementoption.Keep rules determine how to handle the elements that should not be converted, i.e. rendered as HTML in the Markdown output. 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
keepReplacementoption.Remove rules determine which elements to remove altogether. By default, no elements are removed.
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
defaultReplacementoption. -
-
Rule Precedence
Turndown iterates over the set of rules, and picks the first one that matches the
filter. The following list describes the order of precedence:- Blank rule
- Added rules (optional)
- Commonmark rules
- Keep rules
- Remove rules
- Default rule
-
-
Plugins
The plugin API provides a convenient way for developers to apply multiple extensions. A plugin is just a function that is called with the
TurndownServiceinstance.
-
-
tewst
-
replacementFunctionThe replacement function determines how an element should be converted. It should return the Markdown string for a given node. The function is passed the node’s content, the node itself, and the
TurndownServiceoptions.The following rule shows how
\<em\>elements are converted:rules.emphasis = { filter: ['em', 'i'], replacement: function (content, node, options) { return options.emDelimiter + content + options.emDelimiter } } -
Special Rules
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 theblankReplacementoption.Keep rules determine how to handle the elements that should not be converted, i.e. rendered as HTML in the Markdown output. 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
keepReplacementoption.Remove rules determine which elements to remove altogether. By default, no elements are removed.
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
defaultReplacementoption. -
Rule Precedence
Turndown iterates over the set of rules, and picks the first one that matches the
filter. The following list describes the order of precedence: -
Blank rule
-
Added rules (optional)
-
Commonmark rules
-
Keep rules
-
Remove rules
-
Default rule
-
-
A More Advanced Example
With the powerful options, you can do hard tasks with Kingfisher in a simple way. For example, the code below:
-
Downloads a high-resolution image.
-
Downsamples it to match the image view size.
-
Makes it round cornered with a given radius.
-
Shows a system indicator and a placeholder image while downloading.
-
When prepared, it animates the small thumbnail image with a “fade in” effect.
-
The original large image is also cached to disk for later use, to get rid of downloading it again in a detail view.
-
A console log is printed when the task finishes, either for success or failure.