wooorm / lowlight

Virtual syntax highlighting for virtual DOMs and non-HTML things
MIT License
746 stars 26 forks source link

Add `lowlight.registered` #40

Closed fengzilong closed 3 years ago

fengzilong commented 3 years ago

In the following case

// register 'js' as alias of `javascript`
lowlight.registerAlias( 'javascript', 'js' )

// I want to know whether `js` is registered
const hasJSLang = lowlight.listLanguages().indexOf( 'js' ) > -1

// `hasJSLang` is `false` here
if ( hasJSLang ) {
  lowlight.highlight( 'js', code )
} else {
  lowlight.highlightAuto( code )
}

lowhight.listLanguages() doesn't include alias, so there is no method exposed to tell us whether an alias is registered

This problem can be solved by exposing lowlight.getLanguage

const hasJSLang = Boolean( lowlight.getLanguage( 'js' ) ) // -> true

BTW, I found someone use highlight.listLanguages to build a Select component in browser, so I decide to send a PR here rather than highlight.js repository

wooorm commented 3 years ago
  1. I don’t really see a case for why people need to access to a grammar
  2. .getLanguage resolving an alias might be unintuitive?

Would perhaps a registered(aliasOrLanguage): boolean work?

fengzilong commented 3 years ago

It makes sense for me. Already rename to registered and return a boolean type

joshgoebel commented 3 years ago

I'm spoiled by Wren lately but I'd suggest isRegistered. :-)

fengzilong commented 3 years ago

I'm spoiled by Wren lately but I'd suggest isRegistered. :-)

Both is ok for me 😉. @wooorm What's your opinion on the naming?

wooorm commented 3 years ago

@joshgoebel What’s “Wren”?


It’s a bit of an unimportant stylistic choice, whether to prefix with is* or has*. I’ve done that before but personally am more in favor of without the prefix. It’s unambiguous enough in my opinion!

wooorm commented 3 years ago

@fengzilong Could you add a test? And docs?

joshgoebel commented 3 years ago

https://wren.io

@wooorm I'm truly spoiled by Ruby where we have registered?... I think isRegistered (to me) has a bit more of that "flavor"...

fengzilong commented 3 years ago

@fengzilong Could you add a test? And docs?

I'll add test & docs soon when I get off work

fengzilong commented 3 years ago

@fengzilong Could you add a test? And docs?

Test and docs added

fengzilong commented 3 years ago

updated

wooorm commented 3 years ago

Thanks @fengzilong! (and @joshgoebel)