marked icon indicating copy to clipboard operation
marked copied to clipboard

move most options to Hooks

Open UziTech opened this issue 4 years ago • 6 comments
trafficstars

Description

Move most options to Hooks.

This PR might not get merged until v3 but I wanted to throw it out there and see what people think.

The idea behind this is to add a way for extensions to hook into different parts of marked to do things like (sanitizing, escaping, highlighting, etc.) and allowing us to move a lot of the options to extensions.

Example:

marked.use({
  hooks: {
    preprocess(markdown) {
      // Process markdown before marked
    }

    postprocess(html) {
      // Process HTML after marked is finished
    }

    sanitize(html) {
      // Sanitize HTML
      // replace sanitize and sanitizer options
    }

    smartypants(text) {
      // smarty pants
      // replace smartypants option
    }

    error(ex) {
      // Handle marked error
      // replaces silent option
    }

    escape(text) {
      // escape html entities for attributes
    }

    unescape(html) {
      // Unescape header text for id
    }

    languageClass(lang) {
      // set code class
      // replaces langPrefix option
    }

    encode(text) {
      // encode html entities for code blocks
    }

    headerId(text, slugger) {
      // create header id
      // replace headerIds option
    }

    cleanUrl(href) {
      // clean href
      // replace baseUrl option
    }

    mangle(text) {
      // mangle email address
      // replace mangle option
    }

    highlight(code, lang, callback) {
      // highlight code
      // replace highlight option
    }
  }
})

TODO

  • [ ] docs
  • [ ] create hooks tests
  • [ ] create extensions
  • [ ] map old options to new hooks and alert user to use extensions

UziTech avatar Dec 15 '20 02:12 UziTech

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/markedjs/markedjs/1q7bppk1q
✅ Preview: https://markedjs-git-hooks.markedjs.now.sh

vercel[bot] avatar Dec 15 '20 02:12 vercel[bot]

I'm all for more extendability. This seems fine to me, just skimming through, but maybe you @UziTech could help me understand better what this does differently than the existing .use and .options?

calculuschild avatar Dec 19 '20 03:12 calculuschild

For one it adds more opportunities suggested for extensibility in https://github.com/markedjs/marked/discussions/1232#discussioncomment-168005 (namely preprocess and postprocess) it also allows more flexibility for these hooks.

The current highlight and sanitizer are overwritten if multiple extensions provide one. hooks are able to fallback to the next option if they return false. (sidenote: I was actually thinking about changing hooks, tokenizers, and renderers to take a next parameter that they can call instead of returning false to make it more like middleware)

Also it provides a better way than using options like baseUrl and headerPrefix.

UziTech avatar Dec 19 '20 05:12 UziTech

A middleware style would be interesting.... Would that be more performant too?

calculuschild avatar Dec 21 '20 18:12 calculuschild

Seems like marked is undergoing a lot of internal changes atm. Would now be good time to tackle async renderers (#458) or would it be better to wait until this is finished?

janosh avatar Feb 17 '21 07:02 janosh

It would be a great time to tackle async renderers. This PR is mostly a proof of concept. Sort of to flesh out the idea and see what problems we run into.

UziTech avatar Feb 17 '21 07:02 UziTech

want the lifecycle hooks badly.

currently working on an extension that needs cleanup work:

https://github.com/huaichaow/marked-toc-extension/blob/main/src/index.ts

@UziTech can we prioritize this PR?

huaichaow avatar Feb 08 '23 08:02 huaichaow

@huaichaow ya I think the preprocess and postprocess hooks would be the most beneficial I'll start with just those. I'll try to get a PR for that this week.

UziTech avatar Feb 08 '23 13:02 UziTech

@huaichaow I'm not completely done with it but #2730 is what I am thinking. Feel free to review it and leave comments.

UziTech avatar Feb 11 '23 17:02 UziTech