npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

streaming-markdown

v0.2.15

Published

Streaming Markdown parser, à la ChatGPT

Readme

Streaming Markdown

version github

Experiment making a streaming makdown parser à la ChatGPT.


Installation

Install streaming-markdown package from npm.

npm install streaming-markdown

Or just copy smd.js file to your project.

Or use the CDN link.
It's a minified (3kB Gzip) version of the package, with only the necessary functions exported.
See the exports in smd_min_entry.js.
The package uses ES module exports, so you need to use type="module" in your script tag.

<script type="module">
    import * as smd from "https://cdn.jsdelivr.net/npm/streaming-markdown/smd.min.js"
    // ...
</script>

Usage

First create new markdown Parser by calling parser function.
It's single argument is a Renderer object, which is an interface to render the parsed markdown tokens to the DOM.
There are two built-in renderers—default_renderer and logger_renderer—that you can try at first.

import * as smd from "streaming-markdown"

const element  = document.getElementById("markdown")
const renderer = smd.default_renderer(element)
const parser   = smd.parser(renderer)

write function

Then, you can start streaming markdown to the Parser by calling parser_write function with the chunk of markdown string.

smd.parser_write(parser, "# Streaming Markdown\n\n")

You can write as many times as you want to stream the markdown.

The parser is optimistic. When it sees the start of an inline code block or code block, it will immediately style the element accordingly.

E.g. `print("hello wor should be rendered as <code>print("hello wor</code>

While the text is streamed in, the user should be able to select the text that has already been streamed in and copy it. (The parser is only adding new elements to the DOM, not modifying the existing ones.)

end function

Finally, you can end the stream by calling end function.

It will reset the Parser state and flush the remaining markdown.

smd.parser_end(parser)

Renderer interface

| Field name | Type | Description | | ----------- | ---------------------- | ----------- | | data | T | User data object.Available as first param in callbacks. | | add_token | Renderer_Add_Token<T>| When the tokens starts. | | end_token | Renderer_End_Token<T>| When the token ends. | | add_text | Renderer_Add_Text<T> | To append text to current token.Can be called multiple times or none. | | set_attr | Renderer_Set_Attr<T> | Set additional attributes of current token eg. the link url. |

Markdown features

  • [x] Paragraphs
  • [x] Line breaks
    • [x] don't end tokens
    • [x] Escaping line breaks
  • [x] Trim unnecessary spaces
  • [x] Headers
    • [ ] ~~Alternate syntax~~ (not planned)
  • [x] Code Block with indent
  • [x] Code Block with triple backticks
    • [x] language attr
    • [x] with many backticks
  • [x] `inline code` with backticks
    • [x] with many backticks
    • [x] trim spaces code
  • [x] italic with single asterisks
  • [x] Bold with double asterisks
  • [x] italic with underscores
  • [x] Bold with double underscores
  • [x] Special cases:
    • [x] boldbold>em
    • [x] bold>embold
    • [x] emem>bold
    • [x] bold>emem
  • [x] * or _ cannot be surrounded by spaces
  • [x] Strikethrough ~~example~~
  • [x] Escape characters (e.g. * or _ with \* or \_)
  • [x] [Link](url)
    • [x] href attr
  • [ ] Raw URLs
  • [x] Autolinks
  • [ ] Reference-style Links
  • [x] Images
    • [x] src attr
  • [x] Horizontal rules
    • [x] With ---
    • [x] With ***
    • [x] With ___
  • [x] Unordered lists
  • [x] Ordered lists
    • [x] start attr
  • [x] Task lists
  • [x] Nested lists
  • [ ] One-line nested lists
  • [ ] Adding Elements in Lists
  • [x] Blockquotes
  • [x] Tables
    • [ ] Align cols right/center
    • [ ] Multiline cells
  • [ ] Subscript
  • [ ] Superscript
  • [ ] Emoji Shortcodes
  • [ ] Html tags (e.g. <div>, <span>, <a>, <img>, etc.)
    • [x] Line breaks <br>, <br/>
  • [x] LaTex tags for blocks \[...\], $$...$$ and inline \(...\) or $...$

If you think that something is missing or doesn't work, please make an issue.

🔴Ⓜ️⬇️