Skip to content

Simple wrapper utility around unist syntax tree utils (mdast/hast)

License

Notifications You must be signed in to change notification settings

alexwkleung/eva-st-util

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Eva-ST-Util

Simple wrapper utility around unist syntax tree utils.

I wanted a more efficient way of using these utilities without having to install a chain of dependencies and setting up the parser every time I want to work with Markdown.

This is ESM-only.

Installation

Install via npm, directly from the repository.

npm install https://github.com/alexwkleung/Eva-ST-Util

Import Eva-ST-Util.

import { EvaSTUtil } from 'eva-st-util'

Functions

  1. Converting Markdown to HTML.
//type signature
static MDtoHTML_ST(dataString: string): string
  1. Converting HTML to Markdown.
//type signature
static HTMLtoMarkdown_ST(dataString: string): string
  1. Get a tree containing frontmatter nodes.
//type signature
static getFrontmatterTree_ST(dataString: string): Root<any>
  1. Traverse a tree.
//type signature
static traverseTree_ST(tree: Root<any>, type: string): string[]
  1. Convert Markdown to hast
//type signature
static MDtoHast_ST(markdown: string): HastNode<any>
  1. Convert HTML to mdast
//type signature
static HTMLtoMdast_ST(html: string): Root<any>
  1. Convert Markdown to mdast
//type signature
static MDtoMdast_ST(markdown: string): Root<any>
  1. Convert HTML to hast
//type signature
static HTMLtoHast_ST(html: string): Root<any>
  1. Convert Markdown to HTML without sanitization.
//type signature
static MDtoHTMLNoSanitization_ST(markdown: string): string

Go to Example Usage to see how to use these functions.

For converting Markdown to HTML, these are the packages that it uses:

For converting HTML to Markdown, these are the packages that it uses:

For getting a tree with Frontmatter nodes, these are the packages that it uses:

For traversing over a tree, these are the packages that it uses:

Other packages:

For any explanation of what these packages do, you must refer to their respective documentation pages listed above.

Example Usage

Note (1): Your string must contain properly serialized HTML/Markdown or else it may not return the desired output.

Note (2): If you are using math syntax (KaTeX), you'll need the katex.css or katex.min.css stylesheet and the fonts from the fonts folder. These assets can be found in any downloaded KaTeX release.

Markdown to HTML:

import { EvaSTUtil } from 'eva-st-util'

const MarkdownToHTML = EvaSTUtil.MDtoHTML_ST("<your string containing markdown>");

console.log(MarkdownToHTML);

If this was stored in a string from your markdown file:

# header 1
hello world

Then it would return this:

<h1>header 1</h1>
<p>hello world</p>

HTML to Markdown:

import { EvaSTUtil } from 'eva-st-util'

const HTMLtoMarkdown = EvaSTUtil.HTMLtoMarkdown_ST("<your string containing html nodes>");

console.log(HTMLtoMarkdown);

If this was stored in a string from your HTML document:

<h1>header 1<h1>
<p>hello world</p>

Then it would return this:

# header 1
hello world

Other examples will be added later.

License

MIT License.

Releases

No releases published

Packages

No packages published