Socket
Socket
Sign inDemoInstall

mdast-util-from-span-markdown

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mdast-util-from-span-markdown

Minimalistic parser (TypeScript) for span elements written in markdown


Version published
Weekly downloads
31
increased by55%
Maintainers
1
Weekly downloads
 
Created
Source

mdast-util-from-span-markdown

The aim of this repo is to have a minimalistic parser (TypeScript) for span elements written in markdown syntax.
The output is an abstract syntax tree mostly compatible to mdast.

Install

npm i mdast-util-from-span-markdown

Use

import fromMarkdown from "mdast-util-from-span-markdown";
const md = "Say **Hello** [World](https://example.com) in `code`! *Thanks!*";

fromMarkdown(md);

Return

[
  {
    type: "text",
    value: "Say ",
  },
  {
    type: "strong",
    children: [
      {
        type: "text",
        value: "Hello",
      },
    ],
  },
  {
    type: "text",
    value: " ",
  },
  {
    type: "link",
    url: "https://example.com",
    children: [
      {
        type: "text",
        value: "World",
      },
    ],
  },
  {
    type: "text",
    value: " in ",
  },
  {
    type: "inlineCode",
    value: "code",
  },
  {
    type: "text",
    value: "! ",
  },
  {
    type: "emphasis",
    children: [
      {
        type: "text",
        value: "Thanks!",
      },
    ],
  },
];

Technical decission

  1. The code has to be as small as possible (regex instead of regular parsing)
  2. The output has to be as «correct» as possible according to mast specs and CommonMark

Why

Because this is not a full markdown parser (see remark otherwise) with support for block elements, this code is especially handy for «one-line markdown». Think for example about translations comming from a system that does not support rich text formatting.

Render

If you like to render the output of fromMarkdown() in a frontend-framework have a look at the examples/md.tsx for react or examples/md.vue for vue.
The examples are not included in the npm-package. You need to get a copy of the example and adjust it to your needs:

# react
curl https://raw.githubusercontent.com/signalwerk/mdast-util-from-span-markdown/main/examples/md.tsx > md.tsx

# vue
curl https://raw.githubusercontent.com/signalwerk/mdast-util-from-span-markdown/main/examples/md.vue > md.vue

Thank you

FAQs

Package last updated on 21 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc