Skip to content

Commit

Permalink
add commit input
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Feb 14, 2019
1 parent 3681bcf commit 0f4ac74
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
54 changes: 45 additions & 9 deletions docs/demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ onunhandledrejection = function (e) {

var $markdownElem = document.querySelector('#markdown');
var $markedVerElem = document.querySelector('#markedVersion');
var $commitVerElem = document.querySelector('#commitVersion');
$commitVerElem.style.display = 'none';
var $markedVer = document.querySelector('#markedCdn');
var $optionsElem = document.querySelector('#options');
var $outputTypeElem = document.querySelector('#outputType');
Expand All @@ -35,6 +37,7 @@ var $activeOutputElem = null;
var search = searchToObject();

var markedVersions = {
commit: 'commit',
master: 'https://cdn.jsdelivr.net/gh/markedjs/marked/lib/marked.js'
};
var markedVersionCache = {};
Expand Down Expand Up @@ -76,11 +79,7 @@ fetch('https://data.jsdelivr.com/v1/package/npm/marked')
.then(function () {
if ('version' in search && search.version) {
if (!(search.version in markedVersions)) {
markedVersions[search.version] = 'https://cdn.jsdelivr.net/gh/markedjs/marked@' + search.version + '/lib/marked.js';
var opt = document.createElement('option');
opt.textContent = search.version.substring(0, 7);
opt.value = search.version;
$markedVerElem.insertBefore(opt, $markedVerElem.firstChild);
addCommitVersion(search.version);
}
$markedVerElem.value = search.version;
} else {
Expand All @@ -106,6 +105,17 @@ fetch('./quickref.md')
document.querySelector('#quickref').value = text;
});

function addCommitVersion(version) {
if (version in markedVersions) {
return;
}
markedVersions[version] = 'https://cdn.jsdelivr.net/gh/markedjs/marked@' + version + '/lib/marked.js';
var opt = document.createElement('option');
opt.textContent = version.substring(0, 7);
opt.value = version;
$markedVerElem.insertBefore(opt, $markedVerElem.firstChild);
}

function handleInputChange() {
handleChange($inputPanes, $inputTypeElem.value);
}
Expand All @@ -132,7 +142,14 @@ $outputTypeElem.addEventListener('change', handleOutputChange, false);
handleOutputChange();
$inputTypeElem.addEventListener('change', handleInputChange, false);
handleInputChange();
$markedVerElem.addEventListener('change', updateVersion, false);
$markedVerElem.addEventListener('change', function () {
if ($markedVerElem.value === 'commit') {
$commitVerElem.style.display = '';
} else {
$commitVerElem.style.display = 'none';
updateVersion();
}
}, false);

function handleInput() {
inputDirty = true;
Expand All @@ -148,9 +165,25 @@ $optionsElem.addEventListener('keyup', handleInput, false);
$optionsElem.addEventListener('keypress', handleInput, false);
$optionsElem.addEventListener('keydown', handleInput, false);

$commitVerElem.addEventListener('keypress', function (e) {
if (e.which === 13) {
var commit = $commitVerElem.value.toLowerCase();
if (!commit.match(/^[0-9a-f]{40}$/)) {
alert('That is not a valid commit');
return;
}
addCommitVersion(commit);
$markedVerElem.value = commit;
$commitVerElem.style.display = 'none';
$commitVerElem.value = '';
updateVersion();
}
}, false);

$clearElem.addEventListener('click', function () {
$markdownElem.value = '';
$markedVerElem.value = 'master';
$commitVerElem.style.display = 'none';
updateVersion().then(setDefaultOptions);
}, false);

Expand Down Expand Up @@ -264,7 +297,7 @@ function updateVersion() {
var delayTime = 1;
var checkChangeTimeout = null;
function checkForChanges() {
if (inputDirty && (typeof marked !== 'undefined' || (useWorker))) {
if (inputDirty && (typeof marked !== 'undefined' || (useWorker)) && $markedVerElem.value !== 'commit') {
inputDirty = false;

updateLink();
Expand Down Expand Up @@ -384,15 +417,18 @@ function messageWorker(message) {
error = err;
}
}
error = error.replace(/^Uncaught Error: /, '');
$previewElem.classList.add('error');
$htmlElem.classList.add('error');
$lexerElem.classList.add('error');
setParsed(error, error);
setScrollPercent(0);
};
}
markedWorker.working = true;
workerTimeout(0);
if (message.task !== 'defaults') {
markedWorker.working = true;
workerTimeout(0);
}
markedWorker.postMessage(message);
}

Expand Down
4 changes: 3 additions & 1 deletion docs/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ <h1>Marked Demo</h1>
<a id="permalink">Permalink</a> ·
<span>Version: </span>
<select id="markedVersion">
<option value="commit">Add Commit:</option>
<option value="master">master</option>
</select> ·
</select>
<input type="text" id="commitVersion" title="Add commit version" />·
<button id="clear">Clear</button>
<select id="inputType">
<option value="markdown">Markdown</option>
Expand Down
22 changes: 18 additions & 4 deletions docs/demo/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ onmessage = function (e) {
parse(e);
} else {
getVersion(e.data.version).then(function (text) {
// eslint-disable-next-line no-new-func
Function(text)();
try {
// eslint-disable-next-line no-new-func
Function(text)();
} catch (err) {
throw new Error('Cannot load that version of marked');
}
currentVersion = e.data.version;

parse(e);
Expand All @@ -22,8 +26,18 @@ onunhandledrejection = function (e) {
function parse(e) {
switch (e.data.task) {
case 'defaults':
var defaults = marked.getDefaults();
defaults.renderer = null;

var defaults = {};
if (typeof marked.getDefaults === 'function') {
defaults = marked.getDefaults();
delete defaults.renderer;
} else if ('defaults' in marked) {
for (var prop in marked.defaults) {
if (prop !== 'renderer') {
defaults[prop] = marked.defaults[prop];
}
}
}
postMessage({
task: e.data.task,
defaults: defaults
Expand Down

0 comments on commit 0f4ac74

Please sign in to comment.