Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] add options to demo page #1242

Closed
wants to merge 1 commit into from
Closed
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
62 changes: 56 additions & 6 deletions docs/demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ if (!window.fetch) {
window.fetch = unfetch;
}

var $version = document.querySelector('#version');

var $inputElem = document.querySelector('#input');
var $outputTypeElem = document.querySelector('#outputType');
var $previewElem = document.querySelector('#preview');
Expand All @@ -15,6 +17,11 @@ var $clearElem = document.querySelector('#clear');
var $htmlElem = document.querySelector('#html');
var $lexerElem = document.querySelector('#lexer');
var $panes = document.querySelectorAll('.pane');
var $specUrls = document.querySelectorAll('.spec-url');

var $spec = document.querySelector('#spec');
var $smartypants = document.querySelector('#smartypants');

var inputDirty = true;
var $activeElem = null;
var changeTimeout = null;
Expand All @@ -40,13 +47,27 @@ if (search.outputType) {
$outputTypeElem.value = search.outputType;
}

if (search.spec) {
$spec.value = search.spec;
}

if (search.smartypants) {
$smartypants.checked = true;
}

fetch('./quickref.md')
.then(function (res) { return res.text(); })
.then(function (text) {
document.querySelector('#quickref').value = text;
});

function handleChange() {
fetch('https://cdn.jsdelivr.net/npm/marked/package.json')
.then(function (res) { return res.json(); })
.then(function (json) {
$version.textContent = 'v' + json.version;
});

function handleOutputTypeChange() {
for (var i = 0; i < $panes.length; i++) {
$panes[i].style.display = 'none';
}
Expand All @@ -56,13 +77,28 @@ function handleChange() {
updateLink();
};

$outputTypeElem.addEventListener('change', handleChange, false);
handleChange();
$outputTypeElem.addEventListener('change', handleOutputTypeChange, false);
handleOutputTypeChange();

function handleSpecChange() {
for (var i = 0; i < $specUrls.length; i++) {
$specUrls[i].style.display = 'none';
}
var $activeSpec = document.querySelector('#' + $spec.value + '-spec-url');
$activeSpec.style.display = 'inline';
}

function handleInput() {
inputDirty = true;
};

$spec.addEventListener('change', function () {
handleSpecChange();
handleInput();
}, false);
handleSpecChange();

$smartypants.addEventListener('change', handleInput, false);
$inputElem.addEventListener('change', handleInput, false);
$inputElem.addEventListener('keyup', handleInput, false);
$inputElem.addEventListener('keypress', handleInput, false);
Expand Down Expand Up @@ -121,12 +157,20 @@ function setScrollPercent(percent) {
};

function updateLink() {
var outputType = '';
var href = '';
if ($outputTypeElem.value !== 'preview') {
outputType = 'outputType=' + $outputTypeElem.value + '&';
href += 'outputType=' + $outputTypeElem.value + '&';
}

if ($spec.value) {
href += 'spec=' + $spec.value + '&';
}

if ($smartypants.checked) {
href += 'smartypants=1&';
}

$permalinkElem.href = '?' + outputType + 'text=' + encodeURIComponent($inputElem.value);
$permalinkElem.href = '?' + href + 'text=' + encodeURIComponent($inputElem.value);
history.replaceState('', document.title, $permalinkElem.href);
}

Expand All @@ -137,6 +181,12 @@ function checkForChanges() {

updateLink();

marked.setOptions({
pedantic: $spec.value === 'pedantic',
gfm: $spec.value === 'gfm',
smartypants: $smartypants.checked
});

var startTime = new Date();

var scrollPercent = getScrollPercent();
Expand Down
21 changes: 18 additions & 3 deletions docs/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,30 @@
<a href="../">
<img src="../img/logo-black.svg" height="64px" width="64px" />
</a>
<h1>Marked Demo</h1>
<h1>Marked Demo <span id="version"></span></h1>
</header>

<div class="containers">
<div class="container">
<div class="label">
<span>Input</span> ·
<a id="permalink">Permalink</a> ·
<span>Input:</span>
<a id="permalink">Permalink</a>
·
<button id="clear">Clear</button>
·
<select id="spec" title="Specification">
<option value="cm">CommonMark</option>
<option value="gfm">GFM</option>
<option value="pedantic">Pedantic</option>
</select>
<a href="https://spec.commonmark.org/0.28/" id="cm-spec-url" class="spec-url" title="CommonMark Specs">?</a>
<a href="https://github.github.com/gfm/" id="gfm-spec-url" class="spec-url" title="GitHub Flavored Markdown Specs">?</a>
<a href="https://daringfireball.net/projects/markdown/syntax" id="pedantic-spec-url" class="spec-url" title="Original Specs">?</a>
·
<label>
<input type="checkbox" id="smartypants" title="SmartyPants" />SmartyPants
<a href="https://daringfireball.net/projects/smartypants/" title="What is SmartyPants?">?</a>
</label>
</div>
<textarea id="input"></textarea>
</div>
Expand Down