DavidDarnes,
@DavidDarnes@mastodon.design avatar

Idea: Web Component which uses the CSS Highlight API to highlight code blocks

Jbasoo,
@Jbasoo@mastodon.social avatar

@DavidDarnes I think @bramus had a post doing something similar. Can't remember if it was wrapped in a web component though.

DavidDarnes,
@DavidDarnes@mastodon.design avatar

@Jbasoo @bramus most likely the same thing I saw which was the API demo as it's very new

bramus,
@bramus@front-end.social avatar
DavidDarnes,
@DavidDarnes@mastodon.design avatar

@bramus @Jbasoo are you trying to nerd snipe me, because it's working 🥺

bramus,
@bramus@front-end.social avatar
passle,
@passle@mastodon.social avatar

@bramus @DavidDarnes @Jbasoo This is cool, I recently created a syntax highlighting web component at work for our internal wiki thing, but it uses Shiki, not CSS Highlight.

Looking at Shiki, they do have a codeToTokens function, but it requires some internal (great naming 😀) thing to get passed to it which supposedly needs to initialize the langs/themes and I can't be bothered to figure that out right now, but seems like it should be doable

passle,
@passle@mastodon.social avatar
passle,
@passle@mastodon.social avatar

@bramus @DavidDarnes @Jbasoo Hm, can anybody point out to me what I'm doing wrong here? I would have expected 4 different colors in the output, but im seeing only 2?

https://lit.dev/playground/#project=W3sibmFtZSI6InNpbXBsZS1ncmVldGluZy5qcyIsImNvbnRlbnQiOiJpbXBvcnQgeyBjb2RlVG9Ub2tlbnMgfSBmcm9tICdzaGlraS9idW5kbGUvd2ViJztcblxuY29uc3QgeyB0b2tlbnMgfSA9IGF3YWl0IGNvZGVUb1Rva2Vucyhjb2RlLnRleHRDb250ZW50LCB7IFxuICB0aGVtZTogJ25vcmQnLCBcbiAgbGFuZzogJ2pzJ1xufSk7XG5cbmNvbnN0IGNvbG9yR3JvdXBzID0gdG9rZW5zLmZsYXQoKS5yZWR1Y2UoKGFjYywgY3VycikgPT4ge1xuICBjb25zdCByYW5nZSA9IG5ldyBSYW5nZSgpO1xuICByYW5nZS5zZXRTdGFydChjb2RlLmZpcnN0Q2hpbGQsIGN1cnIub2Zmc2V0KTtcbiAgcmFuZ2Uuc2V0RW5kKGNvZGUuZm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IGNvbG9yOiBncmVlbjtcbiAgICB9XG4gICAgXG4gICAgOjpoaWdobGlnaHQoZCkge1xuICAgICAgY29sb3I6IHB1cnBsZTtcbiAgICB9XG4gIDwvc3R5bGU-XG48L2hlYWQ-XG48Ym9keT5cbiAgPGRpdiBpZD1cImNvZGVcIj5cbiAgICBjb25zdCBmb28gPSAxO1xuICAgIGNvbnN0IGJhciA9IDI7XG4gIDwvZGl2PlxuICA8c2ltcGxlLWdyZWV0aW5nIG5hbWU9XCJXb3JsZFwiPjwvc2ltcGxlLWdyZWV0aW5nPlxuPC9ib2R5PlxuIn0seyJuYW1lIjoicGFja2FnZS5qc29uIiwiY29udGVudCI6IntcbiAgXCJkZXBlbmRlbmNpZXNcIjoge1xuICAgIFwibGl0XCI6IFwiXjMuMC4wXCIsXG4gICAgXCJAbGl0L3JlYWN0aXZlLWVsZW1lbnRcIjogXCJeMi4wLjBcIixcbiAgICBcImxpdC1lbGVtZW50XCI6IFwiXjQuMC4wXCIsXG4gICAgXCJsaXQtaHRtbFwiOiBcIl4zLjAuMFwiXG4gIH1cbn0iLCJoaWRkZW4iOnRydWV9XQ

image/png

passle,
@passle@mastodon.social avatar

@bramus @DavidDarnes @Jbasoo Oh lol, figured it out. i++ was in the wrong place, and the fourth color/token is whitespace

This is working nicely then! Very cool api, and nice to see that it can be integrated with shiki pretty easily. Would just have to adjust the stylesheet for the theme to accommodate, but you can probably automate that.

https://lit.dev/playground/#project=W3sibmFtZSI6InNpbXBsZS1ncmVldGluZy5qcyIsImNvbnRlbnQiOiJpbXBvcnQgeyBjb2RlVG9Ub2tlbnMgfSBmcm9tICdzaGlraS9idW5kbGUvd2ViJztcblxuY29uc3QgeyB0b2tlbnMgfSA9IGF3YWl0IGNvZGVUb1Rva2Vucyhjb2RlLnRleHRDb250ZW50LCB7IFxuICB0aGVtZTogJ25vcmQnLCBcbiAgbGFuZzogJ2pzJ1xufSk7XG5cbmNvbnN0IGNvbG9yR3JvdXBzID0gdG9rZW5zLmZs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U-XG4gICAgOjpoaWdobGlnaHQoYSkge1xuICAgICAgY29sb3I6IHJlZDtcbiAgICB9XG4gICAgXG4gICAgOjpoaWdobGlnaHQoYikge1xuICAgICAgY29sb3I6IGJsdWU7XG4gICAgfVxuICAgIFxuICAgIDo6aGlnaGxpZ2h0KGMpIHtcbiAgICAgIGNvbG9yOiBncmVlbjtcbiAgICB9XG4gICAgXG4gICAgOjpoaWdobGlnaHQoZCkge1xuICAgICAgY29sb3I6IHB1cnBsZTtcbiAgICB9XG4gIDwvc3R5bGU-XG48L2hlYWQ-XG48Ym9keT5cbiAgPGRpdiBpZD1cImNvZGVcIj5cbiAgICBjb25zdCBmb28gPSAxO1xuICAgIGNvbnN0IGJhciA9IDI7XG4gIDwvZGl2PlxuICA8c2ltcGxlLWdyZWV0aW5nIG5hbWU9XCJXb3JsZFwiPjwvc2ltcGxlLWdyZWV0aW5nPlxuPC9ib2R5PlxuIn0seyJuYW1lIjoicGFja2FnZS5qc29uIiwiY29udGVudCI6IntcbiAgXCJkZXBlbmRlbmNpZXNcIjoge1xuICAgIFwibGl0XCI6IFwiXjMuMC4wXCIsXG4gICAgXCJAbGl0L3JlYWN0aXZlLWVsZW1lbnRcIjogXCJeMi4wLjBcIixcbiAgICBcImxpdC1lbGVtZW50XCI6IFwiXjQuMC4wXCIsXG4gICAgXCJsaXQtaHRtbFwiOiBcIl4zLjAuMFwiXG4gIH1cbn0iLCJoaWRkZW4iOnRydWV9XQ

scrwd,
@scrwd@mastodon.social avatar

@passle Shiki web bundle seems to be over 4MB? Is it possible to just to bring in the tokenizer bit?

passle,
@passle@mastodon.social avatar

@scrwd havent looked at those things yet - make it work, then make it pretty

DavidDarnes,
@DavidDarnes@mastodon.design avatar

@passle @scrwd I was about to say the same thing, though this is cool regardless! I'm sure a more lean tokenizer can be found. Had a play with it myself but I'm not familiar with the highlight API yet, need to read more

scrwd,
@scrwd@mastodon.social avatar

@DavidDarnes @passle I think it will be interesting when this is targeted directly by libs - so we might just see lean tokenisers maybe - right now using an full existing lib that can run on the server, in the browser seems like overkill for syntax highlighting on a blog - maybe it's cool for interactive tools tho. Aside from a smaller amount of elements in the DOM I'm not sure where the big benefits lay - like is that enough?

DavidDarnes,
@DavidDarnes@mastodon.design avatar

@scrwd @passle one thing I like about it is how portable it is as a possible web component. Relying on the server means having access to edit the markup in some way

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • modclub
  • DreamBathrooms
  • InstantRegret
  • magazineikmin
  • cubers
  • GTA5RPClips
  • thenastyranch
  • Youngstown
  • rosin
  • slotface
  • tacticalgear
  • ethstaker
  • kavyap
  • Durango
  • anitta
  • everett
  • Leos
  • provamag3
  • mdbf
  • ngwrru68w68
  • cisconetworking
  • tester
  • osvaldo12
  • megavids
  • khanakhh
  • normalnudes
  • JUstTest
  • lostlight
  • All magazines