{"id":16525902,"url":"https://github.com/thetarnav/streaming-markdown","last_synced_at":"2025-04-05T11:04:32.028Z","repository":{"id":222360367,"uuid":"755676177","full_name":"thetarnav/streaming-markdown","owner":"thetarnav","description":"🔴Ⓜ️⬇️ Streaming markdown à la ChatGPT (WIP)","archived":false,"fork":false,"pushed_at":"2025-03-26T09:41:39.000Z","size":427,"stargazers_count":98,"open_issues_count":3,"forks_count":7,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-03-28T17:02:32.500Z","etag":null,"topics":["chatbot","html","js","markdown","parser"],"latest_commit_sha":null,"homepage":"https://thetarnav.github.io/streaming-markdown/","language":"JavaScript","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/thetarnav.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,"publiccode":null,"codemeta":null}},"created_at":"2024-02-10T19:50:16.000Z","updated_at":"2025-03-28T16:27:54.000Z","dependencies_parsed_at":"2024-02-13T20:03:42.000Z","dependency_job_id":"78933bad-fb29-4534-bbc0-3ad2a0ef9aa5","html_url":"https://github.com/thetarnav/streaming-markdown","commit_stats":{"total_commits":213,"total_committers":2,"mean_commits":106.5,"dds":"0.028169014084507005","last_synced_commit":"80e7c7c9b78d22a9f5642b5bb5bafad319287f65"},"previous_names":["thetarnav/streaming-markdown"],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thetarnav%2Fstreaming-markdown","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thetarnav%2Fstreaming-markdown/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thetarnav%2Fstreaming-markdown/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thetarnav%2Fstreaming-markdown/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thetarnav","download_url":"https://codeload.github.com/thetarnav/streaming-markdown/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247325694,"owners_count":20920714,"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":["chatbot","html","js","markdown","parser"],"created_at":"2024-10-11T17:07:53.105Z","updated_at":"2025-04-05T11:04:31.989Z","avatar_url":"https://github.com/thetarnav.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Streaming *Markdown*\n\n[![version](https://img.shields.io/npm/v/streaming-markdown?logo=npm)](https://www.npmjs.com/package/streaming-markdown) [![github](https://img.shields.io/badge/GitHub-streaming--markdown-orange?logo=github)](https://github.com/thetarnav/streaming-markdown)\n\n**Experiment making a streaming makdown parser *à la ChatGPT.***\n\n---\n\n## Installation\n\nInstall `streaming-markdown` package from npm.\n\n```bash\nnpm install streaming-markdown\n```\n\n*Or just copy [**`smd.js`**](https://github.com/thetarnav/streaming-markdown/blob/main/smd.js) file to your project.*\n\nOr use the [CDN link](https://www.jsdelivr.com/package/npm/streaming-markdown).\\\nIt's a minified *(3kB Gzip)* version of the package, with only the necessary functions exported.\\\nSee the exports in [`smd_min_entry.js`](https://github.com/thetarnav/streaming-markdown/blob/main/smd_min_entry.js).\\\nThe package uses ES module exports, so you need to use `type=\"module\"` in your script tag.\n\n```html\n\u003cscript type=\"module\"\u003e\n    import * as smd from \"https://cdn.jsdelivr.net/npm/streaming-markdown/smd.min.js\"\n    // ...\n\u003c/script\u003e\n```\n\n## Usage\n\nFirst create new markdown `Parser` by calling `parser` function.\\\nIt's single argument is a `Renderer` object, which is an interface to render the parsed markdown tokens to the DOM.\\\nThere are two built-in renderers—`default_renderer` and `logger_renderer`—that you can try at first.\n\n```js\nimport * as smd from \"streaming-markdown\"\n\nconst element  = document.getElementById(\"markdown\")\nconst renderer = smd.default_renderer(element)\nconst parser   = smd.parser(renderer)\n```\n\n### `write` function\n\nThen, you can start streaming markdown to the `Parser` by calling `parser_write` function with the chunk of markdown string.\n\n```js\nsmd.parser_write(parser, \"# Streaming Markdown\\n\\n\")\n```\n\n*You can write **as many times as you want** to stream the markdown.*\n\nThe parser is optimistic.\nWhen it sees the start of an inline code block or code block,\nit will immediately style the element accordingly.\n\nE.g. `` `print(\"hello wor `` should be rendered as `\u003ccode\u003eprint(\"hello wor\u003c/code\u003e`\n\nWhile the text is streamed in, the user should be able to select the text that has already been streamed in and copy it.\n*(The parser is only adding new elements to the DOM, not modifying the existing ones.)*\n\n### `end` function\n\nFinally, you can end the stream by calling `end` function.\n\nIt will reset the `Parser` state and flush the remaining markdown.\n\n```js\nsmd.parser_end(parser)\n```\n\n### Renderer interface\n\n| Field name  | Type                   | Description |\n| ----------- | ---------------------- | ----------- |\n| `data`      | `T`                    | User data object.\u003cbr\u003eAvailable as first param in callbacks. |\n| `add_token` | `Renderer_Add_Token\u003cT\u003e`| When the tokens starts. |\n| `end_token` | `Renderer_End_Token\u003cT\u003e`| When the token ends. |\n| `add_text`  | `Renderer_Add_Text\u003cT\u003e` | To append text to current token.\u003cbr\u003eCan be called multiple times or none. |\n| `set_attr`  | `Renderer_Set_Attr\u003cT\u003e` | Set additional attributes of current token eg. the link url. |\n\n## Markdown features\n\n- [x] Paragraphs\n- [x] Line breaks\n    - [x] don't end tokens\n    - [x] Escaping line breaks\n- [x] Trim unnecessary spaces\n- [x] Headers\n    - [ ] ~~Alternate syntax~~ *(not planned)*\n- [x] Code Block with indent\n- [x] Code Block with triple backticks\n    - [x] language attr\n    - [x] with many backticks\n- [x] `` `inline code` `` with backticks\n    - [x] with many backticks\n    - [x] trim spaces ` code `\n- [x] *italic* with single asterisks\n- [x] **Bold** with double asterisks\n- [x] _italic_ with underscores\n- [x] __Bold__ with double underscores\n- [x] Special cases:\n    - [x] **bold*bold\u003eem***\n    - [x] ***bold\u003eem*bold**\n    - [x] *em**em\u003ebold***\n    - [x] ***bold\u003eem**em*\n- [x] \\* or \\_ cannot be surrounded by spaces\n- [x] Strikethrough ~~example~~\n- [x] Escape characters (e.g. \\* or \\_ with \\\\\\* or \\\\\\_)\n- [x] \\[Link\\](url)\n    - [x] `href` attr\n- [ ] Raw URLs\n    - [ ] http://example.com\n    - [ ] https://example.com\n    - [ ] www.example.com\n    - [ ] example@fake.com\n    - [ ] mailto:example@fake.com\n- [x] Autolinks\n    - [ ] www.example.com\n    - [x] http://example.com\n    - [x] https://example.com\n    - [ ] example@fake.com\n    - [ ] mailto:example@fake.com\n- [ ] Reference-style Links\n- [x] Images\n    - [x] `src` attr\n- [x] Horizontal rules\n    - [x] With `---`\n    - [x] With `***`\n    - [x] With `___`\n- [x] Unordered lists\n- [x] Ordered lists\n    - [x] `start` attr\n- [x] Task lists\n- [x] Nested lists\n- [ ] One-line nested lists\n- [ ] Adding Elements in Lists\n- [x] Blockquotes\n- [x] Tables\n    - [ ] Align cols right/center\n- [ ] Subscript\n- [ ] Superscript\n- [ ] Emoji Shortcodes\n- [ ] Html tags (e.g. `\u003cdiv\u003e`, `\u003cspan\u003e`, `\u003ca\u003e`, `\u003cimg\u003e`, etc.)\n    - [x] Line breaks `\u003cbr\u003e`, `\u003cbr/\u003e`\n- [x] LaTex tags for blocks `\\[...\\]`, `$$...$$` and inline `\\(...\\)` or `$...$`\n\nIf you think that something is missing or doesn't work, please [make an issue](https://github.com/thetarnav/streaming-markdown/issues).\n\n🔴Ⓜ️⬇️\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthetarnav%2Fstreaming-markdown","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthetarnav%2Fstreaming-markdown","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthetarnav%2Fstreaming-markdown/lists"}