From b149834b794612a1c70a4d0a162f307967a47382 Mon Sep 17 00:00:00 2001 From: Sander Boom Date: Sun, 10 Dec 2017 01:19:15 +0100 Subject: [PATCH] Add image-preview script. This script does two things: 1) Display the image with an user defined width; 2) Wrap the image in an anchor tag that links to the full size image. TODO: do not resize the image when it does not have a width attribute AND is smaller than the maxWidth. --- image-preview/image-preview.qml | 85 +++++++++++++++++++++++++++++++++ image-preview/info.json | 9 ++++ 2 files changed, 94 insertions(+) create mode 100644 image-preview/image-preview.qml create mode 100644 image-preview/info.json diff --git a/image-preview/image-preview.qml b/image-preview/image-preview.qml new file mode 100644 index 0000000..0dfe8f3 --- /dev/null +++ b/image-preview/image-preview.qml @@ -0,0 +1,85 @@ +import QtQml 2.0 + +QtObject { + property int maxWidth; + + property variant settingsVariables: [{ + "identifier": "maxWidth", + "name": "Max width for image preview", + "description": "Note that when the original image width is unknown (e.g. when using Markdown-it), images with a smaller width than this setting will be blown-up. This can not be prevented right now.", + "type": "integer", + "default": "640", + }]; + + /** + * This function is called when the markdown html of a note is generated + * + * It allows you to modify this html + * This is for example called before by the note preview + * + * @param {Note} note - the note object + * @param {string} html - the html that is about to being rendered + * @return {string} the modfied html or an empty string if nothing should be modified + */ + function noteToMarkdownHtmlHook(note, html) { + /** + * This script does two things: + * 1) Display the image with an user defined width; + * 2) Wrap the image in an anchor tag that links to the full size image. + * TODO: do not resize the image when it does not have a width attribute + * AND is smaller than the maxWidth. Wrapping an image in a table with + * `` does not work. + */ + html = html.replace(/]*)/g, handleImg); + + // script.log(html); + return html; + } + + /** + * Handle image. + * @param imgTag + * @returns {string} + */ + function handleImg(imgTag) { + var src = getImgSrc(imgTag); + imgTag = handleWidth(imgTag); + return '' + imgTag + '> maxWidth) { + imgTag = imgTag.replace(widthMatch[0], 'width="' + maxWidth); + } + } + // No width attribute, add one. + // TODO: blows up the image when original is smaller than maxWidth. + // This occurs when using Markdown-it for example. + else { + imgTag = imgTag.replace('