question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom highlight function doesn't handle ${VAR}

See original GitHub issue

Here’s an example with a shell script its quite common to use ${VAR} syntax inside a bash/shell script e.g:

```bash
VAR="MY_VAR"
echo "${MY_VAR}"
```

It looks like this conflicts with MDsveX/Svelte template syntax and never loads for me. This also affects javascript code blocks. Maybe making a note in the docs would make sense in https://github.com/pngwn/MDsveX/issues/210.

@pngwn noted a workaround by using a custom escaping function:

const shiki = require("shiki");

const escape_svelty = (str) =>
  str
    .replace(
      /[{}`]/g,
      (c) => ({ "{": "{", "}": "}", "`": "`" }[c])
    )
    .replace(/\\([trn])/g, "\$1");

async function highlighter(code, lang) {
  const highlighter = await shiki.getHighlighter({ theme: "github-dark" });
  return escape_svelty(highlighter.codeToHtml(code, lang || "text"));
}

module.exports = {
  extensions: [".svx", ".md"],
  highlight: {
    highlighter: highlighter,
  },
};

_Originally posted by @michael0liver in https://github.com/pngwn/MDsveX/issues/205#issuecomment-803667947_

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
pngwncommented, Apr 28, 2021

Released in 0.9.1.

0reactions
pngwncommented, Apr 28, 2021

I don’t think an escape boolean option will work because we don’t want to escape all of the code that is returned from the custom highlight function.

In this case:

const code = `<pre>${highlight(code)}</pre>`

We want to keep the pre tag intact and unescaped (we want them to be interpreted as HTML). I think the solution to this is to export a utility function that people can use to escape the svelte syntax.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to enable element highlighting for handlebars in Sublime ...
I have selected the Handlebars option in the footer in Sublime Text 3, but when I click on an element, it isn't highlighted...
Read more >
CSS Custom Highlight API Module Level 1
The Custom Highlight API provides a programmatic way of adding and removing highlights that do not affect the underlying DOM structure, ...
Read more >
Syntax Highlight Guide | Visual Studio Code Extension API
Syntax Highlight Guide. Syntax highlighting determines the color and style of source code displayed in the Visual Studio Code editor. It is responsible...
Read more >
Highlight patterns and trends with conditional formatting
You can use conditional formatting to highlight cells that contain values that meet a certain condition, or format a whole cell range and...
Read more >
Straightforward way to highlight a single bar in a BarChart
A colour function will do that but easier simply to use a wrapper ... As a not-so-straightforward-yet-fun alternative, you can use a custom...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found