{"id":13621878,"url":"https://github.com/mdnice/sitdown","last_synced_at":"2025-10-10T01:14:38.433Z","repository":{"id":39244446,"uuid":"230475507","full_name":"mdnice/sitdown","owner":"mdnice","description":"turn html to nice markdown","archived":false,"fork":false,"pushed_at":"2024-01-10T00:05:19.000Z","size":4349,"stargazers_count":290,"open_issues_count":11,"forks_count":35,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-02-12T00:03:48.846Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://sitdown.mdnice.com/","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mdnice.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2019-12-27T16:20:40.000Z","updated_at":"2025-01-24T18:33:36.000Z","dependencies_parsed_at":"2023-02-08T17:46:11.454Z","dependency_job_id":"c2506a72-c202-47e3-9499-4cc94b92adcd","html_url":"https://github.com/mdnice/sitdown","commit_stats":{"total_commits":178,"total_committers":4,"mean_commits":44.5,"dds":0.2528089887640449,"last_synced_commit":"f73570a7b225c1aa97b2f49598e29f7858045355"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdnice%2Fsitdown","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdnice%2Fsitdown/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdnice%2Fsitdown/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdnice%2Fsitdown/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mdnice","download_url":"https://codeload.github.com/mdnice/sitdown/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241335593,"owners_count":19946086,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T21:01:11.561Z","updated_at":"2025-10-10T01:14:33.377Z","avatar_url":"https://github.com/mdnice.png","language":"HTML","readme":"# SitDown\nConvert HTML into Markdown with JavaScript.Support GitHub Flavored Markdown Spec.Also support almost wechat/zhihu/csdn/juejin HTML.\n\nLearn from [turndown](https://github.com/domchristie/turndown).\n\nThis project was bootstrapped with [TSDX](https://github.com/jaredpalmer/tsdx).\n\n## Installation\nnpm:\n\n    npm install sitdown\n    \n\n## Usage\n```js\n// For Node\nvar { Sitdown } = require('sitdown')\n\nvar sitdown = new Sitdown()\nvar markdown = sitdown.HTMLToMD('\u003ch1\u003eHello world!\u003c/h1\u003e')\n```\n\n```js\n// For Browser\nimport { Sitdown } from 'sitdown/src.esm'\n\nvar sitdown = new Sitdown()\nvar markdown = sitdown.HTMLToMD('\u003ch1\u003eHello world!\u003c/h1\u003e')\n```\n\nSitdown also accepts DOM nodes as input \\(either element nodes, document nodes, or document fragment nodes\\):\n\n```js\nvar markdown = sitdown.HTMLToMD(document.getElementById('content'))\n```\n\n## Options\nOptions can be passed in to the constructor on instantiation. For example:\n\n```js\nvar sitdown = new Sitdown({ option: 'value' })\n```\n\n| Option | Valid values | Default |\n| :-- | :-- | :-- |\n| `headingStyle` | `setext` or `atx` | `atx` |\n| `hr` | Any [Thematic break](http://spec.commonmark.org/0.27/#thematic-breaks) | `* * *` |\n| `bulletListMarker` | `-`, `+`, or `*` | `*` |\n| `codeBlockStyle` | `indented` or `fenced` | `indented` |\n| `fence` | ` ``` ` or `~~~` | ` ``` ` |\n| `emDelimiter` | `_` or `*` | `_` |\n| `strongDelimiter` | `**` or `__` | `**` |\n| `linkStyle` | `inlined` or `referenced` | `inlined` |\n| `linkReferenceStyle` | `full`, `collapsed`, or `shortcut` | `full` |\n| `keepFilter` | `style`, `['style','div']`, or a function | `['style','div']` |\n| `env` | `{}` |  |\n\n## Advanced Options\n| Option | Valid values | Default |\n| :-- | :-- | :-- |\n| `blankReplacement` | rule replacement function | See **Special Rules** below |\n| `keepReplacement` | rule replacement function | See **Special Rules** below |\n| `defaultReplacement` | rule replacement function | See **Special Rules** below |\n\n## Methods\n\n### [](#addrulekey-rule)`addRule(key, rule)`\n\nThe `key` parameter is a unique name for the rule for easy reference. Example:\n\n```js\nsitdown.service.addRule('strikethrough', {\n  filter: ['del', 's', 'strike'],\n  replacement: function (content) {\n    return '~' + content + '~'\n  }\n})\n```\n\n`addRule` returns the `service` instance for chaining.\n\nSee **Extending with Rules** below.\n\n### `keep(filter)`\n\nDetermines which elements are to be kept and rendered as HTML. By default, Sitdown does not keep any elements. The filter parameter works like a rule filter \\(see section on filters belows\\). Example:\n\n```js\nsitdown.service.keep(['del', 'ins'])\nsitdown.HTMLToMD('\u003cp\u003eHello \u003cdel\u003eworld\u003c/del\u003e\u003cins\u003eWorld\u003c/ins\u003e\u003c/p\u003e') // 'Hello \u003cdel\u003eworld\u003c/del\u003e\u003cins\u003eWorld\u003c/ins\u003e'\n```\n\nThis will render `\u003cdel\u003e` and `\u003cins\u003e` elements as HTML when converted.\n\n`keep` can 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.\n\n`keep` returns the `service` instance for chaining.\n\n### [](#removefilter)`remove(filter)`\n\nDetermines which elements are to be removed altogether i.e. converted to an empty string. By default, Sitdown does not remove any elements. The filter parameter works like a rule filter \\(see section on filters belows\\). Example:\n\n```js\nsitdown.service.remove('del')\nsitdown.HTMLToMD('\u003cp\u003eHello \u003cdel\u003eworld\u003c/del\u003e\u003cins\u003eWorld\u003c/ins\u003e\u003c/p\u003e') // 'Hello World'\n```\n\nThis will remove `\u003cdel\u003e` elements \\(and contents\\).\n\n`remove` can 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.\n\n`remove` returns the `service` instance for chaining.\n\n### [](#usepluginarray)`use(plugin|array)`\n\nUse a plugin, or an array of plugins. Example:\n\n```js\nimport { Sitdown } from 'sitdown';\nimport { applyJuejinRule } from '@sitdown/juejin';\n\nlet sitdown = new Sitdown({\n      keepFilter: ['style'],\n      codeBlockStyle: 'fenced',\n      bulletListMarker: '-',\n      hr: '---',\n});\nsitdown.use(applyJuejinRule);\n```\n\n`use` returns the `service` instance for chaining.\n\nSee **Plugins** below.\n\n## [](#extending-with-rules)Extending with Rules\n\nSitdown can be extended by adding **rules**. A rule is a plain JavaScript object with `filter` and `replacement` properties. For example, the rule for converting `\u003cp\u003e` elements is as follows:\n\n```js\n{\n  filter: 'p',\n  replacement: function (content) {\n    return '\\n\\n' + content + '\\n\\n'\n  }\n}\n```\n\nThe filter selects `\u003cp\u003e` elements, and the replacement function returns the `\u003cp\u003e` contents separated by two new lines.\n\n### [](#filter-stringarrayfunction)`filter` String|Array|Function\n\nThe 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:\n\n* `filter: 'p'` will select `\u003cp\u003e` elements\n* `filter: ['em', 'i']` will select `\u003cem\u003e` or `\u003ci\u003e` elements\n\nAlternatively, 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 `Service` options. For example, the following rule selects `\u003ca\u003e` elements \\(with an `href`\\) when the `linkStyle` option is `inlined`:\n\n```js\nfilter: function (node, options) {\n  return (\n    options.linkStyle === 'inlined' \u0026\u0026\n    node.nodeName === 'A' \u0026\u0026\n    node.getAttribute('href')\n  )\n}\n```\n\n### [](#replacement-function)`replacement` Function\n\nThe 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 `Service` options.\n\nThe following rule shows how `\u003cem\u003e` elements are converted:\n\n```js\nrules.emphasis = {\n  filter: ['em', 'i'],\n\n  replacement: function (content, node, options) {\n    return options.emDelimiter + content + options.emDelimiter\n  }\n}\n```\n\n### [](#special-rules)Special Rules\n\n**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 `\u003ca\u003e`, `\u003ctd\u003e`,`\u003cth\u003e` or a void element. Its behaviour can be customised using the `blankReplacement` option.\n\n**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 `keepReplacement` option.\n\n**Remove rules** determine which elements to remove altogether. By default, no elements are removed.\n\n**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.\n\n### [](#rule-precedence)Rule Precedence\n\nSitdown iterates over the set of rules, and picks the first one that matches the `filter`. The following list describes the order of precedence:\n\n1.  Blank rule\n2.  Added rules \\(optional\\)\n3.  Commonmark rules\n4.  Keep rules\n5.  Remove rules\n6.  Default rule\n\n## [](#plugins)Plugins\n\nThe plugin API provides a convenient way for developers to apply multiple extensions. A plugin is just a function that is called with the `service` instance.\n\n## Escaping Markdown Characters\nSitdown 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 `\u003ch1\u003e1. Hello world\u003c/h1\u003e` needs to be escaped to `1\\. Hello world`, otherwise it will be interpreted as a list item rather than a heading.\n\nTo avoid the complexity and the performance implications of parsing the content of every HTML element as Markdown, Sitdown uses a group of regular expressions to escape potential Markdown syntax. As a result, the escaping rules can be quite aggressive.","funding_links":[],"categories":["HTML"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmdnice%2Fsitdown","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmdnice%2Fsitdown","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmdnice%2Fsitdown/lists"}