Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh(parser) post-highlightBlock callbacks via plugin #2285

Merged
merged 15 commits into from
Jan 31, 2020

Conversation

joshgoebel
Copy link
Member

@joshgoebel joshgoebel commented Nov 14, 2019

  • add tests

First pass at simple plugin API to allow things like:

    hljs.addPlugin( {
      "after:highlightBlock": ({block, result}) => {
        // move the language from the result into the dataset
        block.dataset.language = result.language }
    })

I realize I need to document the plugin API itself and the callbacks, but how does this look at a high level? We'd also support classes for more complex plugins:

  class DataLanguagePlugin {
    constructor(options) {
      self.prefix = options.dataPrefix;
    }

    afterHighlightBlock({block, result}) {
      // ...
    }
  }

  hljs.addPlugin(DataLanguagePlugin, { dataPrefix: "hljs"})

Passing a class being the preferred way for more complex things, and the object based approach serves for one-liners to make them easy to whip up.

@joshgoebel joshgoebel changed the title post-highlightBlock callbacks via plugin WIP: post-highlightBlock callbacks via plugin Nov 14, 2019
@joshgoebel joshgoebel added enhancement An enhancement or new feature parser labels Nov 14, 2019
Copy link
Collaborator

@egor-rogov egor-rogov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't yet tried it, just had a look, but generally I like it. See a couple of comments in code.

@joshgoebel joshgoebel self-assigned this Dec 6, 2019
@joshgoebel
Copy link
Member Author

@egor-rogov How you feeling about this in general? Can I polish it up for merging? (which requires building out the class functionality, writing some tests, better docs.)

I'll ping you again for a final review, but I wanted to make sure you're cool with the naming before I put more effort into this.

@egor-rogov
Copy link
Collaborator

How you feeling about this in general? Can I polish it up for merging? (which requires building out the class functionality, writing some tests, better docs.)

Definitely yes.
My only concern is events-functions mapping. I feel it's important to use only events names in plugin code.

@joshgoebel
Copy link
Member Author

joshgoebel commented Dec 8, 2019

Don't we resolve that by just saying "always use the event names", as suggested above:

  class DataLanguagePlugin {
    constructor(options) {
      this.prefix = options.dataPrefix;
    }

    'after:highlightBlock'({block, result}) {
      // ...
    }
  }

@egor-rogov
Copy link
Collaborator

Don't we resolve that by just saying "always use the event names"

We do.

@joshgoebel
Copy link
Member Author

I want to review the tests here and perhaps add a few more but is this otherwise good to merge?

@joshgoebel joshgoebel changed the title WIP: post-highlightBlock callbacks via plugin enh(parser) post-highlightBlock callbacks via plugin Jan 15, 2020
@joshgoebel
Copy link
Member Author

@egor-rogov Ping?

Copy link
Collaborator

@egor-rogov egor-rogov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for delay.
I like it!

@joshgoebel joshgoebel merged commit 07a93c5 into highlightjs:master Jan 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement or new feature parser
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants