{"id":18099367,"url":"https://github.com/ts-stack/markdown","last_synced_at":"2025-04-04T17:10:57.128Z","repository":{"id":46008962,"uuid":"113714356","full_name":"ts-stack/markdown","owner":"ts-stack","description":"A full-featured markdown parser and compiler, written in TypeScript.","archived":false,"fork":false,"pushed_at":"2023-08-26T00:25:19.000Z","size":1013,"stargazers_count":142,"open_issues_count":0,"forks_count":28,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-28T16:11:09.506Z","etag":null,"topics":["gfm","javascript","markdown","markdown-flavors","markdown-parser","typescript"],"latest_commit_sha":null,"homepage":"","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ts-stack.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"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,"publiccode":null,"codemeta":null}},"created_at":"2017-12-10T01:40:37.000Z","updated_at":"2025-02-20T19:27:43.000Z","dependencies_parsed_at":"2024-06-18T14:00:07.167Z","dependency_job_id":null,"html_url":"https://github.com/ts-stack/markdown","commit_stats":null,"previous_names":["kostyatretyak/marked-ts"],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ts-stack%2Fmarkdown","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ts-stack%2Fmarkdown/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ts-stack%2Fmarkdown/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ts-stack%2Fmarkdown/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ts-stack","download_url":"https://codeload.github.com/ts-stack/markdown/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246701017,"owners_count":20819977,"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":["gfm","javascript","markdown","markdown-flavors","markdown-parser","typescript"],"created_at":"2024-10-31T21:08:05.979Z","updated_at":"2025-04-04T17:10:57.106Z","avatar_url":"https://github.com/ts-stack.png","language":"HTML","readme":"# @ts-stack/markdown\n\n\u003e A full-featured markdown parser and compiler, written in TypeScript.\n\nThis is fork of popular library `marked` from [this commit](https://github.com/markedjs/marked/tree/39fbc8aed)\n(Merge pull request #961 from chjj/release-0.3.7, Dec 1, 2017).\n\n## lang\n- [Chinese](./lang/zh/README.md)\n\n## Table of contents\n\n- [Install](#install)\n- [Usage](#usage)\n  - [Minimal usage](#minimal-usage)\n  - [Example usage with highlight.js](#example-usage-with-highlightjs)\n  - [Overriding renderer methods](#overriding-renderer-methods)\n  - [Example of setting a simple block rule](#example-of-setting-a-simple-block-rule)\n- [Benchmarks](#benchmarks)\n  - [Options for benchmarks](#options-for-benchmarks)\n    - [Example of usage bench options](#example-of-usage-bench-options)\n- [Contribution and License Agreement](#contribution-and-license-agreement)\n- [License](#license)\n\n## Install\n\n``` bash\nnpm install @ts-stack/markdown --save\n```\n\n## Usage\n\n### Minimal usage:\n\n```js\nimport { Marked } from '@ts-stack/markdown';\n\nconsole.log(Marked.parse('I am using __markdown__.'));\n// Outputs: I am using \u003cstrong\u003emarkdown\u003c/strong\u003e.\n```\n\nExample setting options with default values:\n\n```js\nimport { Marked, Renderer } from '@ts-stack/markdown';\n\nMarked.setOptions ({\n  renderer: new Renderer,\n  gfm: true,\n  tables: true,\n  breaks: false,\n  pedantic: false,\n  sanitize: false,\n  smartLists: true,\n  smartypants: false\n});\n\nconsole.log(Marked.parse('I am using __markdown__.'));\n```\n\n### Example usage with highlight.js\n\n```bash\nnpm install highlight.js --save\n```\n\nA function to highlight code blocks:\n\n```ts\nimport { Marked } from '@ts-stack/markdown';\nimport hljs from 'highlight.js';\n\nMarked.setOptions({ highlight: (code, lang) =\u003e hljs.highlight(lang, code).value });\nlet md = '```js\\n console.log(\"hello\"); \\n```';\nconsole.log(Marked.parse(md));\n```\n\n### Overriding renderer methods\n\nThe renderer option allows you to render tokens in a custom manner. Here is an\nexample of overriding the default heading token rendering by adding custom head id:\n\n```ts\nimport { Marked, Renderer } from '@ts-stack/markdown';\n\nclass MyRenderer extends Renderer {\n  // Overriding parent method.\n  override heading(text: string, level: number, raw: string) {\n    const regexp = /\\s*{([^}]+)}$/;\n    const execArr = regexp.exec(text);\n    let id: string;\n    \n    if(execArr) {\n      text = text.replace(regexp, '');\n      id = execArr[1];\n    } else {\n      id = text.toLocaleLowerCase().replace(/[^\\wа-яіїє]+/gi, '-');\n    }\n\n    return `\u003ch${level} id=\"${id}\"\u003e${text}\u003c/h${level}\u003e`;\n  }\n}\n\nMarked.setOptions({ renderer: new MyRenderer });\n\nconsole.log(Marked.parse('# heading {my-custom-hash}'));\n```\n\nThis code will output the following HTML:\n\n```html\n\u003ch1 id=\"my-custom-hash\"\u003eheading\u003c/h1\u003e\n```\n\n### Example of setting a simple block rule\n\nIf you do not need recursiveness or checks some conditions before execute a regular expression, you can use the\n`Marked.setBlockRule( regexp[, callback] )` method, which takes a regular expression as the first argument,\nand returns result `regexp.exec(string)` to `callback(execArr)`, which can be passed as a second argument.\n\nIn regular expression very important adding symbol `^` from start. You should do this anyway.\n\n```ts\nimport { Marked, escape } from '@ts-stack/markdown';\n\n/**\n * KaTeX is a fast, easy-to-use JavaScript library for TeX math rendering on the web.\n */\nimport * as katex from 'katex';\n\n\nMarked.setBlockRule(/^@@@ *(\\w+)\\n([\\s\\S]+?)\\n@@@/, function (execArr) {\n\n  // Don't use arrow function for this callback\n  // if you need Renderer's context, for example to `this.options`.\n\n  const channel = execArr[1];\n  const content = execArr[2];\n\n  switch(channel) {\n    case 'youtube': {\n      const id = escape(content);\n      return `\\n\u003ciframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/embed/${id}\"\u003e\u003c/iframe\u003e\\n`;\n    }\n    case 'katex': {\n      return katex.renderToString(escape(content));\n    }\n    default: {\n      const msg = `[Error: a channel \"${channel}\" for an embedded code is not recognized]`;\n      return '\u003cdiv style=\"color: red\"\u003e' + msg + '\u003c/div\u003e';\n    }\n  }\n});\n\nconst blockStr = `\n# Example usage with embed block code\n\n@@@ katex\nc = \\\\pm\\\\sqrt{a^2 + b^2}\n@@@\n\n@@@ youtube\nJgwnkM5WwWE\n@@@\n`;\n\nconst html = Marked.parse(blockStr);\n\nconsole.log(html);\n```\n\n## Benchmarks\n\nnode v8.9.x\n\n``` bash\ngit clone https://github.com/ts-stack/markdown.git\ncd markdown\nnpm install\nnpm run bench\n```\n\nBy default, these benchmarks run the entire markdown test suite once. The test suite includes every markdown feature,\nit doesn't cater to specific aspects.\n\n| Lib                     | Lib load, ms | Lib init, ms | Bench work, ms | Total, ms \n| ------------------------|--------------|--------------|----------------|-----------\n| @ts-stack/markdown v1.5.0 | 9          | 5            | 67             | 81 \n| marked v7.0.4           | 30           | 22           | 128            | 180\n| markdown v0.5.0         | 10           | 8            | 180            | 198\n| remarkable v2.0.1       | 22           | 9            | 126            | 157\n| commonmark v0.30.0      | 51           | 2            | 120            | 173\n| markdown-it v13.0.1     | 56           | 3            | 171            | 230\n| showdown v2.1.0         | 18           | 38           | 545            | 601\n\n\n### Options for benchmarks\n\n```text\n-l, --length       Approximate string length in kilobytes. Default ~ 300 KB.\n-t, --times        Number of runs this bench. Default - 1 times.\n```\n\nTest files are accumulated in one file. If you specify, for example, `--length 100`\nthe first file will be taken, checked whether it is longer than 100 kilobytes,\nand if no - it will be attached to the next one and checked its length, and so on.\n\n#### Example of usage bench options\n\nIn order for npm passing the parameters, they need to be separated via ` -- `:\n\n```text\nnpm run bench -- -l 500 -t 1\n```\n\n## Contribution and License Agreement\n\nIf you contribute code to this project, you are implicitly allowing your code\nto be distributed under the MIT license. You are also implicitly verifying that\nall code is your original work. `\u003c/legalese\u003e`\n\n## Other libs from the author\n\nIf you enjoy working with TypeScript, we also recommend other libraries by the same author:\n\n- [Ditsmod](https://ditsmod.github.io/en/) - a web framework for Node.js to build modular applications;\n- [@ts-stack/openapi-spec](https://github.com/ts-stack/openapi-spec) - TypeScript models for writing OpenAPI documentation.\n","funding_links":[],"categories":["HTML"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fts-stack%2Fmarkdown","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fts-stack%2Fmarkdown","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fts-stack%2Fmarkdown/lists"}