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 🙏

© 2024 – Pkg Stats / Ryan Hefner

marked-sequential-hooks

v1.2.1

Published

A marked extension that allows preprocessing and post-processing of Markdown, sequentially

Downloads

499

Readme

marked-sequential-hooks

A marked extension that enables the sequential preprocessing and post-processing of Markdown content. This extension allows you to apply a series of transformations to Markdown or HTML content before or after it is processed.

Install

You can install marked-sequential-hooks using npm or yarn:

npm i marked-sequential-hooks
# or
yarn add marked-sequential-hooks

Usage

Once you've installed this extension, you can use it in your marked configuration. Here's an example of how to configure it:

Node.js

Say we have the following file example.md:

---
title: Hello, world!
author: John Doe
---

# Content

This is the main content of your Markdown file autored by **{author}**.

And our module example.js looks as follows:

import { readFileSync } from 'node:fs'
import { Marked } from 'marked'
import markedSequentialHooks, {
  type HtmlHook,
  type MarkdownHook
} from 'marked-sequential-hooks'
import frontmatter from 'marked-hook-frontmatter'

const myHook: MarkdownHook = (markdown, data) => {
  // now we can access data from frontmatter
  console.log(data)
  // yields: {title: 'Hello, world!', author: 'John Doe'}

  return markdown
}

const layoutHook: HtmlHook = (html, data) => {
  return `<!doctype html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>${data.title}</title>
  </head>

  <body>
    ${html}
  </body>
</html>
`
}

const html = new Marked()
  .use(
    markedSequentialHooks({
      markdownHooks: [frontmatter(), myHook],
      htmlHooks: [layoutHook]
    })
  )
  .parse(readFileSync('example.md', 'utf8'))

console.log(html)

Now, running node example.js yields:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Hello, world!</title>
  </head>

  <body>
    <h1>Content</h1>
    <p>
      This is the main content of your Markdown file autored by
      <strong>John Doe</strong>.
    </p>
  </body>
</html>

Browser

Say we have the following file example.html:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Marked sequential hooks</title>
  </head>
  <body>
    <div id="content"></div>

    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/marked-sequential-hooks/dist/index.umd.min.js"></script>
    <script>
      ;(async () => {
        const md = '# {title}\n\n{body}\n'

        const myAsyncHook = async (markdown, data) => {
          const res = await fetch('https://dummyjson.com/posts/2')

          Object.assign(data, await res.json())

          return markdown
        }

        document.getElementById('content').innerHTML = await new marked.Marked({
          async: true
        })
          .use(
            markedSequentialHooks({
              markdownHooks: [
                myAsyncHook,
                (markdown, data) => {
                  document.title = data.id

                  return markdown
                    .replace('{title}', data.title)
                    .replace('{body}', data.body)
                }
              ]
            })
          )
          .parse(md)
      })()
    </script>
  </body>
</html>

Try marked-sequential-hooks on RunKit

Options

The marked-sequential-hooks extension accepts the following configuration options:

markdownHooks?: MarkdownHook[]

An array of functions to preprocess Markdown content before parsing with marked. Here is an example of how to create a Markdown hook function:

import type { MarkdownHook } from 'marked-sequential-hooks'

/**
 * Represents a Markdown hook function.
 *
 * @param markdown - The Markdown string from the prev MarkdownHook.
 * @param data - The extended Hooks data object.
 * @returns The transformed Markdown string.
 */
const myHook: MarkdownHook = (markdown, data) => {
  // do something with markdown

  return markdown
}

Optionally, you can also pass an options object to your hook function:

import type { MarkdownHook } from 'marked-sequential-hooks'

function myHook(options = {}): MarkdownHook {
  const { foo, bar } = options

  return (markdown, data) => {
    // do something with markdown

    return markdown
  }
}

htmlHooks?: HtmlHook[]

An array of functions to post-process HTML content after parsing with marked. Here is an example of how to create an HTML hook:

import type { HtmlHook } from 'marked-sequential-hooks'

/**
 * Represents an HTML hook function.
 *
 * @param html - The HTML string from the prev HtmlHook.
 * @param data - The extended Hooks data object.
 * @returns The transformed HTML string.
 */
const myHook: HtmlHook = (html, data) => {
  // do something with html

  return html
}

Optionally, you can also pass an options object to your hook function:

import type { HtmlHook } from 'marked-sequential-hooks'

function myHook(options = {}): HtmlHook {
  const { foo, bar } = options

  return (html, data) => {
    // do something with html

    return html
  }
}

Related

Contributing

We 💛  issues.

When committing, please conform to the semantic-release commit standards. Please install commitizen and the adapter globally, if you have not already.

npm i -g commitizen cz-conventional-changelog

Now you can use git cz or just cz instead of git commit when committing. You can also use git-cz, which is an alias for cz.

git add . && git cz

License

GitHub

A project by Stilearning © 2023.