Skip to content

Commit da9d155

Browse files
authored
Merge pull request #1357 from aprotim/demo_options
Add the ability to specify options on the demo page as JSON
2 parents b6d5a67 + e9f57d9 commit da9d155

3 files changed

Lines changed: 80 additions & 26 deletions

File tree

docs/demo/demo.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ header h1 {
4545
box-sizing: border-box;
4646
}
4747

48-
.pane, #input {
48+
#options {
49+
resize: both;
50+
}
51+
52+
.pane, .inputPane {
4953
margin-top: 5px;
5054
padding: 0.6em;
5155
border: 1px solid #ccc;

docs/demo/demo.js

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@ if (!window.fetch) {
77
window.fetch = unfetch;
88
}
99

10-
var $inputElem = document.querySelector('#input');
10+
var $markdownElem = document.querySelector('#markdown');
11+
var $optionsElem = document.querySelector('#options');
1112
var $outputTypeElem = document.querySelector('#outputType');
13+
var $inputTypeElem = document.querySelector('#inputType');
1214
var $previewIframe = document.querySelector('#preview iframe');
1315
var $permalinkElem = document.querySelector('#permalink');
1416
var $clearElem = document.querySelector('#clear');
1517
var $htmlElem = document.querySelector('#html');
1618
var $lexerElem = document.querySelector('#lexer');
1719
var $panes = document.querySelectorAll('.pane');
20+
var $inputPanes = document.querySelectorAll('.inputPane');
1821
var inputDirty = true;
19-
var $activeElem = null;
22+
var $activeOutputElem = null;
2023
var changeTimeout = null;
2124
var search = searchToObject();
2225

@@ -28,13 +31,13 @@ $previewIframe.addEventListener('load', function () {
2831
});
2932

3033
if ('text' in search) {
31-
$inputElem.value = search.text;
34+
$markdownElem.value = search.text;
3235
} else {
3336
fetch('./initial.md')
3437
.then(function (res) { return res.text(); })
3538
.then(function (text) {
36-
if ($inputElem.value === '') {
37-
$inputElem.value = text;
39+
if ($markdownElem.value === '') {
40+
$markdownElem.value = text;
3841
inputDirty = true;
3942
clearTimeout(changeTimeout);
4043
checkForChanges();
@@ -43,6 +46,19 @@ if ('text' in search) {
4346
});
4447
}
4548

49+
if ('options' in search) {
50+
$optionsElem.value = search.options;
51+
} else {
52+
$optionsElem.value = JSON.stringify(
53+
marked.getDefaults(),
54+
function (key, value) {
55+
if (value && typeof value === 'object' && Object.getPrototypeOf(value) !== Object.prototype) {
56+
return undefined;
57+
}
58+
return value;
59+
}, ' ');
60+
}
61+
4662
if (search.outputType) {
4763
$outputTypeElem.value = search.outputType;
4864
}
@@ -53,30 +69,50 @@ fetch('./quickref.md')
5369
document.querySelector('#quickref').value = text;
5470
});
5571

56-
function handleChange() {
57-
for (var i = 0; i < $panes.length; i++) {
58-
$panes[i].style.display = 'none';
59-
}
60-
$activeElem = document.querySelector('#' + $outputTypeElem.value);
61-
$activeElem.style.display = '';
72+
function handleInputChange() {
73+
handleChange($inputPanes, $inputTypeElem.value);
74+
}
6275

76+
function handleOutputChange() {
77+
$activeOutputElem = handleChange($panes, $outputTypeElem.value);
6378
updateLink();
79+
}
80+
81+
function handleChange(panes, visiblePane) {
82+
var active = null;
83+
for (var i = 0; i < panes.length; i++) {
84+
if (panes[i].id === visiblePane) {
85+
panes[i].style.display = '';
86+
active = panes[i];
87+
} else {
88+
panes[i].style.display = 'none';
89+
}
90+
}
91+
return active;
6492
};
6593

66-
$outputTypeElem.addEventListener('change', handleChange, false);
67-
handleChange();
94+
$outputTypeElem.addEventListener('change', handleOutputChange, false);
95+
handleOutputChange();
96+
$inputTypeElem.addEventListener('change', handleInputChange, false);
97+
handleInputChange();
6898

6999
function handleInput() {
70100
inputDirty = true;
71101
};
72102

73-
$inputElem.addEventListener('change', handleInput, false);
74-
$inputElem.addEventListener('keyup', handleInput, false);
75-
$inputElem.addEventListener('keypress', handleInput, false);
76-
$inputElem.addEventListener('keydown', handleInput, false);
103+
$markdownElem.addEventListener('change', handleInput, false);
104+
$markdownElem.addEventListener('keyup', handleInput, false);
105+
$markdownElem.addEventListener('keypress', handleInput, false);
106+
$markdownElem.addEventListener('keydown', handleInput, false);
107+
108+
$optionsElem.addEventListener('change', handleInput, false);
109+
$optionsElem.addEventListener('keyup', handleInput, false);
110+
$optionsElem.addEventListener('keypress', handleInput, false);
111+
$optionsElem.addEventListener('keydown', handleInput, false);
77112

78113
$clearElem.addEventListener('click', function () {
79-
$inputElem.value = '';
114+
$markdownElem.value = '';
115+
$optionsElem.value = '';
80116
handleInput();
81117
}, false);
82118

@@ -110,7 +146,7 @@ function jsonString(input) {
110146
};
111147

112148
function getScrollSize() {
113-
var e = $activeElem;
149+
var e = $activeOutputElem;
114150

115151
return e.scrollHeight - e.clientHeight;
116152
};
@@ -121,10 +157,10 @@ function getScrollPercent() {
121157
return 1;
122158
}
123159

124-
return $activeElem.scrollTop / size;
160+
return $activeOutputElem.scrollTop / size;
125161
};
126162
function setScrollPercent(percent) {
127-
$activeElem.scrollTop = percent * getScrollSize();
163+
$activeOutputElem.scrollTop = percent * getScrollSize();
128164
};
129165

130166
function updateLink() {
@@ -133,11 +169,13 @@ function updateLink() {
133169
outputType = 'outputType=' + $outputTypeElem.value + '&';
134170
}
135171

136-
$permalinkElem.href = '?' + outputType + 'text=' + encodeURIComponent($inputElem.value);
172+
$permalinkElem.href = '?' + outputType + 'text=' + encodeURIComponent($markdownElem.value)
173+
+ '&options=' + encodeURIComponent($optionsElem.value);
137174
history.replaceState('', document.title, $permalinkElem.href);
138175
}
139176

140177
var delayTime = 1;
178+
var options = {};
141179
function checkForChanges() {
142180
if (inputDirty) {
143181
inputDirty = false;
@@ -148,7 +186,14 @@ function checkForChanges() {
148186

149187
var scrollPercent = getScrollPercent();
150188

151-
var lexed = marked.lexer($inputElem.value);
189+
try {
190+
var optionsString = $optionsElem.value || '{}';
191+
var newOptions = JSON.parse(optionsString);
192+
options = newOptions;
193+
} catch (err) {
194+
}
195+
196+
var lexed = marked.lexer($markdownElem.value, options);
152197

153198
var lexedList = [];
154199

@@ -160,7 +205,7 @@ function checkForChanges() {
160205
lexedList.push('{' + lexedLine.join(', ') + '}');
161206
}
162207

163-
var parsed = marked.parser(lexed);
208+
var parsed = marked.parser(lexed, options);
164209

165210
if (iframeLoaded) {
166211
$previewIframe.contentDocument.body.innerHTML = (parsed);

docs/demo/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ <h1>Marked Demo</h1>
2424
<span>Input</span> ·
2525
<a id="permalink">Permalink</a> ·
2626
<button id="clear">Clear</button>
27+
<select id="inputType">
28+
<option value="markdown">Markdown</option>
29+
<option value="options">Options</option>
30+
</select>
2731
</div>
28-
<textarea id="input"></textarea>
32+
<textarea id="markdown" class="inputPane"></textarea>
33+
<textarea id="options" class="inputPane" placeholder="Options (as JSON)"></textarea>
2934
</div>
3035

3136
<div class="container">

0 commit comments

Comments
 (0)