Skip to content

Markdown (md) tagged template literal #895

Open
@mbostock

Description

@mbostock

Here’s a possible implementation:

```js
import markdownit from "npm:markdown-it";
```

```js
const Markdown = new markdownit({html: true});

function md(strings) {
  let string = strings[0];
  for (let i = 1; i < arguments.length; ++i) {
    string += String(arguments[i]);
    string += strings[i];
  }
  const template = document.createElement("template");
  template.innerHTML = Markdown.render(string);
  return template.content.cloneNode(true);
}
```

```js
md`# Hello, ${"world"}!`
```

We don’t currently do this because it’s unsafe — if you interpolate text into the Markdown, it’s interpreted as Markdown or HTML and can affect the meaning of statically declared content (unlike Hypertext Literal, which is safe). Perhaps another option would be to offer an md.unsafe function that makes the unsafe-ness more obvious and could allow us to offer a safe md tagged template literal in the future.

```js
import markdownit from "npm:markdown-it";
```

```js
const Markdown = new markdownit({html: true});

const md = {
  unsafe(string) {
    const template = document.createElement("template");
    template.innerHTML = Markdown.render(string);
    return template.content.cloneNode(true);
  }
};
```

```js
md.unsafe(`# Hello, ${"world"}!`)
```

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions