Skip to content

Instantly share code, notes, and snippets.

@breakin
Created July 12, 2018 15:09
Show Gist options
  • Save breakin/5c7d414d8e63892368bd7686237f55c5 to your computer and use it in GitHub Desktop.
Save breakin/5c7d414d8e63892368bd7686237f55c5 to your computer and use it in GitHub Desktop.
markdeep suggestions
By adding the following small snippet at the top of markdeepToHTML markdeep can sortof support local file insertion when being run in node.js.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function markdeepToHTML(str, elementMode, local_file_handler = undefined) {
// If a local file handler is specified, resolve '(insert src here)' directly when possible'
// Only expected to be used when run as script via node.js
if (local_file_handler !== undefined) {
str = str.rp(/(?:^|\s)\(insert[ \t]+(\S+\.\S*)[ \t]+here\)\s/g, function(match, filename) {
var content = local_file_handler(filename);
if (content === undefined) {
// Leave unchanged!
return match;
} else {
return content;
}
});
}
...
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Using it looks like this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fs = require('fs'); // file system
function local_file_handler(filename) {
var fn = 'docs/' + filename;
try {
var content = fs.readFileSync('docs/' + filename, 'utf8');
console.log(' Successfully inserted ' + fn);
return content;
} catch (err) {
console.log(' Failed to insert ' + fn + ', error ' + err);
return undefined;
}
}
content = window.markdeep.format(data, false, local_file_handler);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Caveat 1 - Assumes utf8 source documents
----------------------------------------
I'm not sure how to detect encoding of source document in node.js and it is not specified in the insert statement.
I should probably open it in ascii and see if there is a meta-tag first.
Caveat 2 - Assumption no markdeep script tag
--------------------------------------------
Currently assumes that there is no markdeep-loading script in the file being inserted.
That is no markdeep-script-tag.
I think it would be nice to solve that here but not sure how to exactly.
If the use case is to support server-side execution from node.js then maybe it is ok to not have it in the source files.
I already assume 2 in the main document for the [offline processor](https://github.com/breakin/markdeep-offline) but I think it would be nice to lift it here.
Caveat 3 - Security etc
-----------------------
The link is not verified so this should only be run on on documents you trust or in a sand-box.
I would probably move the validation to the local_file_handler-function so markdeep itself doesn't have to grow.
@breakin
Copy link
Author

breakin commented Jul 12, 2018

Note that encoding of source document could also fall to the local_file_handler-function. I think the important thing is that it must be well-specified in what encoding the local_file_handler is supposed to return the content in.

@breakin
Copy link
Author

breakin commented Jul 12, 2018

Here is a commit to markdeep-offline breakin/markdeep-offline@5012aeb
It also "fixes" (in a very naive way) caveat 2 by removing any line starting with .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment