docsifyjs/docsify

async compiler

Open

#1.433 geöffnet am 28. Nov. 2020

Auf GitHub ansehen
 (2 Kommentare) (1 Reaktion) (0 zugewiesene Personen)JavaScript (5.630 Forks)batch import
help wantedneeds discussionpr welcome

Repository-Metriken

Stars
 (26.454 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 18T 16h) (9 gemergte PRs in 30 T)

Beschreibung

Feature request

What problem does this feature solve?

Returning a promise from the function passed to vm.config.renderer.code doesn't work as one would expect.

I wanted to use an async method (crypto.subtle.digest in my mermaid plugin:

hook.mounted(function() {
  const renderer = vm.config.markdown.renderer
  const original = renderer.code || renderer.origin.code

  renderer.code = renderer.origin.code = async function(code, lang) {
    if (lang === 'mermaid') {
      const sha256 = Array.from(new Uint8Array(await window.crypto.subtle.digest('SHA-256', new TextEncoder().encode(code)))).map(b => b.toString(16).padStart(2, '0')).join('')
      const mermaidId = `mermaid-svg-${sha256}`
      return `<figure class="visual diagram mermaid">${mermaid.render(mermaidId, code)}</figure>`
    }
    else return original.apply(this, arguments)
  }
})

But all my code fences are now being replaced, in the HTML document, with:

[object Promise]

What does the proposed API look like?

No change, but the compiler should be promise-aware (so that when the result of calling a function is a promise, it awaits it).

How should this be implemented in your opinion?

I'm not sure how the compiler works at the moment.

Are you willing to work on this yourself?

If time allows, which is probably negative :-/

Contributor Guide