docs: support data field in playground.html

This commit is contained in:
harttle
2021-06-20 21:26:16 +08:00
parent 1c64ff051e
commit f7313fe563
5 changed files with 45 additions and 21 deletions
-1
View File
@@ -1,4 +1,3 @@
{ {
"categories": [{ "categories": [{
"color": "red", "color": "red",
+6 -1
View File
@@ -37,7 +37,12 @@ hexo.extend.helper.register('page_nav', function () {
}) })
hexo.extend.helper.register('raw', function (filepath) { hexo.extend.helper.register('raw', function (filepath) {
const content = readFileSync(resolve(this.view_dir, filepath)) const content = readFileSync(resolve(this.view_dir, filepath), 'utf8')
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;')
return content return content
}) })
+5 -3
View File
@@ -2,9 +2,11 @@
<div class="wrapper"> <div class="wrapper">
<h1 class="inner">{{__('playground.title')}}</h1> <h1 class="inner">{{__('playground.title')}}</h1>
<div class="row inner"> <div class="row inner">
<div id="editorEl">{{__('playground.loading')}}</div> <div class="col">
<div id="previewEl">{{__('playground.loading')}}</div> <div id="editorEl">{{ raw('partial/demo.liquid') }}</div>
<div id="dataEl">{}</div>
</div>
<div class="col" id="previewEl">{{__('playground.loading')}}</div>
</div> </div>
<script id="sourceEl" type="text/plain">{{ raw('partial/demo.liquid') }}</script>
</div> </div>
</div> </div>
+12 -7
View File
@@ -11,16 +11,21 @@
flex-wrap: wrap; flex-wrap: wrap;
justify-content: space-between justify-content: space-between
margin-bottom: 30px margin-bottom: 30px
> div font-size: 1em
flex: 0 0 100% .col
height: 400px flex: 1 1 100%
margin-bottom: 30px margin-bottom: 30px
@media mq-normal @media mq-normal
.row .row
flex-wrap: nowrap; flex-wrap: nowrap;
> div .col:first-child
flex: 0 1 49% margin-right: 15px
#editorEl, #previewEl #editorEl
font-size: 1em height: 40vh
#dataEl
height: 28vh
margin-top: 2vh
#previewEl
height: 70vh
+22 -9
View File
@@ -57,17 +57,23 @@
(function() { (function() {
// playground // playground
/* global liquidjs, sourceEl, ace */ /* global liquidjs, ace */
if (!location.pathname.match(/playground.html$/)) return; if (!location.pathname.match(/playground.html$/)) return;
const engine = new liquidjs.Liquid(); const engine = new liquidjs.Liquid();
const editor = createEditor('editorEl', 'liquid'); const editor = createEditor('editorEl', 'liquid');
const dataEditor = createEditor('dataEl', 'json');
const preview = createEditor('previewEl', 'html'); const preview = createEditor('previewEl', 'html');
preview.setReadOnly(true); preview.setReadOnly(true);
preview.renderer.setShowGutter(false); preview.renderer.setShowGutter(false);
preview.renderer.setPadding(20); preview.renderer.setPadding(20);
editor.setValue(getInitialValue(), 1); const init = parseArgs(location.hash.slice(1));
if (init) {
editor.setValue(init.tpl, 1);
dataEditor.setValue(init.data, 1);
}
editor.on('change', update); editor.on('change', update);
dataEditor.on('change', update);
update(); update();
function createEditor(id, lang) { function createEditor(id, lang) {
@@ -82,18 +88,25 @@
return editor; return editor;
} }
function getInitialValue() { function parseArgs(hash) {
if (!hash) return;
try { try {
return atou(location.hash.slice(1)) || sourceEl.textContent let [tpl, data] = hash.split(',').map(atou);
data = data || '{}';
return { tpl, data };
} catch (e) {} } catch (e) {}
return sourceEl.textContent }
function serializeArgs(obj) {
return utoa(obj.tpl) + ',' + utoa(obj.data);
} }
async function update() { async function update() {
const tpl = editor.getValue(); const tpl = editor.getValue();
history.replaceState({}, '', '#' + utoa(tpl)); const data = dataEditor.getValue();
history.replaceState({}, '', '#' + serializeArgs({tpl, data}));
try { try {
const html = await engine.parseAndRender(tpl); const html = await engine.parseAndRender(tpl, JSON.parse(data));
preview.setValue(html, 1); preview.setValue(html, 1);
} catch (err) { } catch (err) {
preview.setValue(err.stack, 1); preview.setValue(err.stack, 1);
@@ -102,11 +115,11 @@
} }
function atou(str) { function atou(str) {
return decodeURIComponent(escape(atob(str))) return decodeURIComponent(escape(atob(str)));
} }
function utoa(str) { function utoa(str) {
return btoa(unescape(encodeURIComponent(str))) return btoa(unescape(encodeURIComponent(str)));
} }
}()); }());