From f439246cd64a5769fa13ecfaa7746045ad12b7fe Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 10 Aug 2023 22:53:13 -0600 Subject: [PATCH 1/7] docs: fix older versions in demo --- build-docs.js | 12 +++++------- docs/demo/worker.js | 36 ++++++++++++++++++++++++------------ 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/build-docs.js b/build-docs.js index 4d3c851873..2c7d499950 100644 --- a/build-docs.js +++ b/build-docs.js @@ -42,15 +42,13 @@ async function build(currentDir, tmpl) { await build(filename, tmpl); } else { // console.log('Reading file ' + filename); - let buffer = await readFile(filename); + let html = await readFile(filename, 'utf8'); const parsed = parse(filename); if (parsed.ext === '.md' && isUppercase(parsed.name)) { - const html = marked.parse(buffer.toString('utf8')); - buffer = Buffer.from(tmpl + const mdHtml = marked.parse(html); + html = tmpl .replace('', getTitle(parsed.name)) - .replace('', html), - 'utf8' - ); + .replace('', mdHtml); parsed.ext = '.html'; parsed.name = parsed.name.toLowerCase(); delete parsed.base; @@ -60,7 +58,7 @@ async function build(currentDir, tmpl) { // console.log('Ensure directory ' + dirname(outfile)); await mkdir(dirname(outfile), { recursive: true }); console.log('Writing file ' + outfile); - await writeFile(outfile, buffer, { mode }); + await writeFile(outfile, html, { mode }); } } } diff --git a/docs/demo/worker.js b/docs/demo/worker.js index 1103d526d8..41096eea34 100644 --- a/docs/demo/worker.js +++ b/docs/demo/worker.js @@ -64,7 +64,8 @@ function parse(e) { const marked = versionCache[currentVersion]; const options = mergeOptions(e.data.options); const startTime = new Date(); - const lexed = marked.lexer(e.data.markdown, options); + // marked 0.0.1 had tokens array as the second parameter and no options + const lexed = marked.lexer(e.data.markdown, currentVersion.endsWith('@0.0.1') ? [] : options); const lexedList = jsonString(lexed); const parsed = marked.parser(lexed, options); const endTime = new Date(); @@ -116,17 +117,28 @@ function loadVersion(ver) { } else { promise = import(ver + '/lib/marked.esm.js') .catch(() => { + const tryReturnMarked = (onError) => (text) => { + const g = globalThis || global; + g.module = { }; + try { + // eslint-disable-next-line no-new-func + Function(text)(); + } catch (err) { + delete g.module; + return onError(); + } + const marked = g.marked || g.module.exports; + return marked; + }; return fetch(ver + '/marked.min.js') - .then(function(res) { return res.text(); }) - .then(function(text) { - try { - // eslint-disable-next-line no-new-func - Function(text)(); - } catch (err) { - throw new Error('No esm or min build'); - } - return (globalThis || global).marked; - }); + .then((res) => res.text()) + .then(tryReturnMarked(() => { + return fetch(ver + '/lib/marked.js') + .then((res) => res.text()) + .then(tryReturnMarked(() => { + throw new Error('No esm or min build'); + })); + })); }) .then((marked) => { if (!marked) { @@ -135,7 +147,7 @@ function loadVersion(ver) { versionCache[ver] = marked.marked; } else if (marked.default) { versionCache[ver] = marked.default; - } else if (marked.parse) { + } else if (marked.lexer && marked.parser) { versionCache[ver] = marked; } else { throw new Error('Cannot find marked'); From b4d7060e1f4fbf3bab4d8c448519bf56436cb414 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 10 Aug 2023 23:00:38 -0600 Subject: [PATCH 2/7] check currentversion before timing --- docs/demo/worker.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/demo/worker.js b/docs/demo/worker.js index 41096eea34..041a9771cb 100644 --- a/docs/demo/worker.js +++ b/docs/demo/worker.js @@ -62,10 +62,10 @@ function parse(e) { } case 'parse': { const marked = versionCache[currentVersion]; - const options = mergeOptions(e.data.options); + // marked 0.0.1 had tokens array as the second parameter of lexer and no options + const options = currentVersion.endsWith('@0.0.1') ? [] : mergeOptions(e.data.options); const startTime = new Date(); - // marked 0.0.1 had tokens array as the second parameter and no options - const lexed = marked.lexer(e.data.markdown, currentVersion.endsWith('@0.0.1') ? [] : options); + const lexed = marked.lexer(e.data.markdown, options); const lexedList = jsonString(lexed); const parsed = marked.parser(lexed, options); const endTime = new Date(); From 060898eb9b36a7129a81d0f1d382d0313aba4efc Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 10 Aug 2023 23:37:32 -0600 Subject: [PATCH 3/7] clean up --- docs/demo/worker.js | 44 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/docs/demo/worker.js b/docs/demo/worker.js index 041a9771cb..9d570500e9 100644 --- a/docs/demo/worker.js +++ b/docs/demo/worker.js @@ -110,36 +110,32 @@ function jsonString(input, level) { } } +function fetchMarked(file) { + return () => + fetch(file) + .then((res) => res.text()) + .then((text) => { + const g = globalThis || global; + g.module = { }; + try { + // eslint-disable-next-line no-new-func + Function(text)(); + } catch (err) { + throw new Error('Cannot find marked.js file'); + } + const marked = g.marked || g.module.exports; + return marked; + }); +} + function loadVersion(ver) { let promise; if (versionCache[ver]) { promise = Promise.resolve(); } else { promise = import(ver + '/lib/marked.esm.js') - .catch(() => { - const tryReturnMarked = (onError) => (text) => { - const g = globalThis || global; - g.module = { }; - try { - // eslint-disable-next-line no-new-func - Function(text)(); - } catch (err) { - delete g.module; - return onError(); - } - const marked = g.marked || g.module.exports; - return marked; - }; - return fetch(ver + '/marked.min.js') - .then((res) => res.text()) - .then(tryReturnMarked(() => { - return fetch(ver + '/lib/marked.js') - .then((res) => res.text()) - .then(tryReturnMarked(() => { - throw new Error('No esm or min build'); - })); - })); - }) + .catch(fetchMarked(ver + '/marked.min.js')) + .catch(fetchMarked(ver + '/lib/marked.js')) .then((marked) => { if (!marked) { throw Error('No marked'); From 529559e1fd079ba1107be17f10a2069980c378c8 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 10 Aug 2023 23:37:39 -0600 Subject: [PATCH 4/7] add loading error --- docs/demo/demo.css | 8 ++++++++ docs/demo/demo.js | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/docs/demo/demo.css b/docs/demo/demo.css index 3b9de0032c..6844952d54 100644 --- a/docs/demo/demo.css +++ b/docs/demo/demo.css @@ -75,6 +75,14 @@ header h1 { background-color: #FEE } +.loadingError { + background-color: #fee; + font-weight: bold; + color: #f00; + text-align: center; + padding: 10px; +} + #responseTime { display: inline-block; } diff --git a/docs/demo/demo.js b/docs/demo/demo.js index 237563805c..6df721a26b 100644 --- a/docs/demo/demo.js +++ b/docs/demo/demo.js @@ -66,6 +66,9 @@ Promise.all([ setScrollPercent(0); $loadingElem.style.display = 'none'; $mainElem.style.display = 'block'; +}).catch(() => { + $loadingElem.classList.add('loadingError'); + $loadingElem.textContent = 'Failed to load marked. Refresh the page to try again.'; }); function setInitialText() { @@ -114,6 +117,7 @@ function setInitialVersion() { markedVersions.master = 'https://cdn.jsdelivr.net/gh/markedjs/marked@' + json[0].sha; }) .catch(function() { + console.warn('Cannot find commits'); // do nothing // uses url without commit }); From 909968063d1e40dc9bc32018b8c25d3bf454b43f Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 10 Aug 2023 23:40:37 -0600 Subject: [PATCH 5/7] show file in error --- docs/demo/worker.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/demo/worker.js b/docs/demo/worker.js index 9d570500e9..9a634c204f 100644 --- a/docs/demo/worker.js +++ b/docs/demo/worker.js @@ -121,7 +121,7 @@ function fetchMarked(file) { // eslint-disable-next-line no-new-func Function(text)(); } catch (err) { - throw new Error('Cannot find marked.js file'); + throw new Error(`Cannot find ${file}`); } const marked = g.marked || g.module.exports; return marked; From 7b58600114ce1043db0247d23d384a953e99cefd Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 10 Aug 2023 23:54:15 -0600 Subject: [PATCH 6/7] add back octo-arm wave --- docs/_document.html | 2 +- docs/css/style.css | 27 +++++++++++++++++++++++++++ docs/demo/demo.css | 35 +++++++++++++++++++++++++++-------- docs/demo/index.html | 4 ++-- 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/docs/_document.html b/docs/_document.html index b8e9356634..c77922af42 100644 --- a/docs/_document.html +++ b/docs/_document.html @@ -8,7 +8,7 @@ - - - <!--{{title}}-->Marked Documentation + diff --git a/docs/css/shared.css b/docs/css/shared.css new file mode 100644 index 0000000000..c71c2fba4d --- /dev/null +++ b/docs/css/shared.css @@ -0,0 +1,26 @@ +.github-corner { + position: absolute; + top: 0; + border: 0; + right: 0; +} + +.github-corner:hover .octo-arm { + animation:octocat-wave 560ms ease-in-out; +} + +@keyframes octocat-wave { + 0%,100%{transform:rotate(0)} + 20%,60%{transform:rotate(-25deg)} + 40%,80%{transform:rotate(10deg)} +} + +@media (max-width:500px) { + .github-corner:hover .octo-arm { + animation:none + } + + .github-corner .octo-arm { + animation:octocat-wave 560ms ease-in-out; + } +} diff --git a/docs/css/style.css b/docs/css/style.css index c4e146c206..2398e0af36 100644 --- a/docs/css/style.css +++ b/docs/css/style.css @@ -186,30 +186,3 @@ pre code { .div-copy.click .tooltip-copy::after { opacity: 1; } - -.github-corner { - position: absolute; - top: 0; - border: 0; - right: 0; -} - -.github-corner:hover .octo-arm { - animation:octocat-wave 560ms ease-in-out; -} - -@keyframes octocat-wave { - 0%,100%{transform:rotate(0)} - 20%,60%{transform:rotate(-25deg)} - 40%,80%{transform:rotate(10deg)} -} - -@media (max-width:500px) { - .github-corner:hover .octo-arm { - animation:none - } - - .github-corner .octo-arm { - animation:octocat-wave 560ms ease-in-out; - } -} diff --git a/docs/demo/demo.css b/docs/demo/demo.css index f7fb106086..30941e1ba8 100644 --- a/docs/demo/demo.css +++ b/docs/demo/demo.css @@ -78,30 +78,3 @@ header h1 { #responseTime { display: inline-block; } - -.github-corner { - position: absolute; - top: 0; - border: 0; - right: 0; -} - -.github-corner:hover .octo-arm { - animation:octocat-wave 560ms ease-in-out; -} - -@keyframes octocat-wave { - 0%,100%{transform:rotate(0)} - 20%,60%{transform:rotate(-25deg)} - 40%,80%{transform:rotate(10deg)} -} - -@media (max-width:500px) { - .github-corner:hover .octo-arm { - animation:none - } - - .github-corner .octo-arm { - animation:octocat-wave 560ms ease-in-out; - } -} diff --git a/docs/demo/index.html b/docs/demo/index.html index 62e143e405..b85a4de926 100644 --- a/docs/demo/index.html +++ b/docs/demo/index.html @@ -4,6 +4,7 @@ Marked Demo +