Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion txt2tags-it/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"script": "txt2tags-it.qml",
"resources": ["markdown-it.js", "markdown-it-txt2tags.js"],
"authors": ["@luginf"],
"version": "0.1",
"version": "0.1.1",
"minAppVersion": "26.4.11",
"description": "This script, based on markdown-it, replaces the default markdown renderer with markdown-it AND also with the txt2tags syntax.\n\n<b>Dependencies</b>\n<a href=\"https://github.com/markdown-it/markdown-it\">markdown-it.js</a> (v8.4.2 bundled with the script)\n\n<b>Usage</b>\nFor the possible configuration options check <a href=\"https://github.com/markdown-it/markdown-it/tree/main/lib/presets\">here</a>.\n\n<b>Important</b>\nThis script currently only works with <a href=\"https://github.com/qownnotes/scripts/issues/77\"><b>legacy media links</b></a>. You can turn them on in the <i>General Settings</i>.\n\nImportant note: You need to use legacy image linking with this script, otherwise there will be no images shown in the preview!"
}
5 changes: 4 additions & 1 deletion txt2tags-it/markdown-it-txt2tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,10 @@ var markdownitTxt2tags;
function (state, silent) {
var pos = state.pos;
var src = state.src;
// Must start with a URL scheme (letters then "://")
// Fast-fail: scheme must start with an ASCII letter
var ch = src.charCodeAt(pos);
if (!((ch >= 0x41 && ch <= 0x5a) || (ch >= 0x61 && ch <= 0x7a)))
return false;
var match = /^[a-zA-Z][\w+\-.]*:\/\/[^\s\]]*/.exec(src.slice(pos));
if (!match) return false;
var url = match[0];
Expand Down
20 changes: 14 additions & 6 deletions txt2tags-it/txt2tags-it.qml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ QtObject {
property bool useTxt2tagsPlugin
property bool useEditorHighlighting
property bool useSetextHeadings
property bool _isWindows: false
property variant _headRe
property variant _urlAttrRe
property string _cssInject: "table {border-spacing: 0; border-style: solid; border-width: 1px; border-collapse: collapse; margin-top: 0.5em;} th, td {padding: 0 5px;} del {text-decoration: line-through;}"

function init() {
var optionsObj = eval("(" + options + ")");
Expand Down Expand Up @@ -85,6 +89,10 @@ QtObject {
script.addHighlightingRule("^%.*$", "%", 11);
}

_isWindows = script.platformIsWindows();
_headRe = /<head>[\s\S]*?<\/head>/;
_urlAttrRe = /(\b(?:src|href|data-[\w-]+)\s*=\s*)(["'])([^"']+)\2/gi;

//Allow file:// url scheme
var validateLinkOrig = md.validateLink;
var GOOD_PROTO_RE = /^(file):/;
Expand Down Expand Up @@ -160,6 +168,7 @@ QtObject {
applyHeading(3);
break;
}
mainWindow.focusNoteTextEdit();
}

/**
Expand All @@ -179,10 +188,11 @@ QtObject {
var mdHtml = md.render(note.noteText);
//Insert root folder in attachments and media relative urls
var path = script.currentNoteFolderPath();
if (script.platformIsWindows())
if (_isWindows)
path = "/" + path;

mdHtml = mdHtml.replace(/(\b(?:src|href|data-[\w-]+)\s*=\s*)(["'])([^"']+)\2/gi, (_, attr, quote, rawPath) => {
_urlAttrRe.lastIndex = 0;
mdHtml = mdHtml.replace(_urlAttrRe, (_, attr, quote, rawPath) => {
if (isProtocolUrl(rawPath))
return `${attr}${quote}${rawPath}${quote}`;

Expand All @@ -194,10 +204,8 @@ QtObject {
return `${attr}${quote}file://${finalPath}${quote}`;
});

//Get original styles
var head = html.match(new RegExp("<head>(?:.|\n)*?</head>"))[0];
//Add custom styles
head = head.replace("</style>", "table {border-spacing: 0; border-style: solid; border-width: 1px; border-collapse: collapse; margin-top: 0.5em;} th, td {padding: 0 5px;} del {text-decoration: line-through;}" + (customStylesheet || "") + "</style>");
var head = html.match(_headRe)[0];
head = head.replace("</style>", _cssInject + (customStylesheet || "") + "</style>");
mdHtml = "<html>" + head + "<body>" + mdHtml + "</body></html>";
return mdHtml;
}
Expand Down
Loading