marktext/src/renderer/services/printService.js
Lee Holmes 5ffc026a28 This change adds several features to make it easier to write Markdown articles that desire server-based paths (such as /images/my_image.png).
1) There is now a new preference page under images for "Maintaining server paths". You specify a server path (such as /images/) and a local path (such as c:\hugo\static\images), and
Marktext will do the right mapping internally. Image previews will come from the images on your computer, while the content stored in the document will represent the corresponding server paths.
Full documentation has been added to IMAGES.md.
2) Path variables (like ${filename}) have been expanded to support year, month, and day. This helps prevent filename collisions and is common in many blogging platforms.
3) Pasting images and dragging them into Marktext supports this path mapping as well, moving them to the local path as appropriate
4) Image imports (pasting and or dragging) now retain the source image file name if possible, only using the image hash if there is a conflict. Images with names are easier to manage and show up
appropriately in search engines
4) The image edit dialog now has a 'Rename' tab so that you can rename the images to something more specific when they are just pasted from the clipboard. This simplifies the previous workflow that
used to require editing the markdown and renaming the file in the filesystem manually.

There is also now a 'User Notification Dialog' component to let Marktext communicate with the user about error conditions.
2024-01-19 18:42:50 -08:00

40 lines
1.1 KiB
JavaScript

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, this.muya.options).src
}
}
document.body.appendChild(printContainer)
}
/**
* Remove the print container from the window.
*/
clearup () {
if (this.container) {
this.container.remove()
}
}
}
export default MarkdownPrint