forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintService.js
More file actions
39 lines (35 loc) · 1.11 KB
/
printService.js
File metadata and controls
39 lines (35 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { getImageInfo } from 'muya/lib/utils'
class MarkdownPrint {
/**
* Prepare document export and append a hidden print container to the window.
*
* @param {string} html HTML string
* @param {boolean} [renderStatic] Render for static files like PDF documents
*/
renderMarkdown (html, renderStatic = false) {
this.clearup()
const printContainer = document.createElement('article')
printContainer.classList.add('print-container')
this.container = printContainer
printContainer.innerHTML = html
// Fix images when rendering for static files like PDF (GH#678).
if (renderStatic) {
// Traverse through the DOM tree and fix all relative image sources.
const images = printContainer.getElementsByTagName('img')
for (const image of images) {
const rawSrc = image.getAttribute('src')
image.src = getImageInfo(rawSrc).src
}
}
document.body.appendChild(printContainer)
}
/**
* Remove the print container from the window.
*/
clearup () {
if (this.container) {
this.container.remove()
}
}
}
export default MarkdownPrint