From 7603972889edb33aaf15983245fed5229b3943b6 Mon Sep 17 00:00:00 2001 From: Hatem Hosny Date: Wed, 1 Jan 2025 20:05:18 +0200 Subject: [PATCH 1/6] feat: allow running code in playground --- source/_posts/javascript.md | 1 + source/_posts/python.md | 1 + themes/coo/layout/_partial/scripts.ejs | 64 ++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/source/_posts/javascript.md b/source/_posts/javascript.md index 392126d..758d3b2 100644 --- a/source/_posts/javascript.md +++ b/source/_posts/javascript.md @@ -11,6 +11,7 @@ intro: | A JavaScript cheat sheet with the most important concepts, functions, methods, and more. A complete quick reference for beginners. plugins: - copyCode + - runCode --- ## Getting Started diff --git a/source/_posts/python.md b/source/_posts/python.md index 53f3220..e0c670b 100644 --- a/source/_posts/python.md +++ b/source/_posts/python.md @@ -11,6 +11,7 @@ intro: | The [Python](https://www.python.org/) cheat sheet is a one-page reference sheet for the Python 3 programming language. plugins: - copyCode + - runCode --- ## Getting Started diff --git a/themes/coo/layout/_partial/scripts.ejs b/themes/coo/layout/_partial/scripts.ejs index 41c0302..7093f53 100644 --- a/themes/coo/layout/_partial/scripts.ejs +++ b/themes/coo/layout/_partial/scripts.ejs @@ -72,3 +72,67 @@ }); <% } %> + + +<% if (is_post() && page.plugins != undefined && page.plugins.includes("runCode") ){ %> + + +<% } %> From d235f88e862583192c0b988404d535c1f2fd19c1 Mon Sep 17 00:00:00 2001 From: Hatem Hosny Date: Sun, 5 Jan 2025 01:51:42 +0200 Subject: [PATCH 2/6] feat: add support for running markdown --- source/_posts/markdown.md | 1 + themes/coo/layout/_partial/scripts.ejs | 55 +++++++++++++++++--------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/source/_posts/markdown.md b/source/_posts/markdown.md index 5be5ff3..4d5655d 100644 --- a/source/_posts/markdown.md +++ b/source/_posts/markdown.md @@ -12,6 +12,7 @@ categories: intro: This is a quick reference cheat sheet to the Markdown syntax. plugins: - copyCode + - runCode --- ## Markdown Quick Reference diff --git a/themes/coo/layout/_partial/scripts.ejs b/themes/coo/layout/_partial/scripts.ejs index 7093f53..49dcbb9 100644 --- a/themes/coo/layout/_partial/scripts.ejs +++ b/themes/coo/layout/_partial/scripts.ejs @@ -91,27 +91,27 @@ parent.style = "position: relative;" parent.insertBefore(runButton, codeBlock); - let content = codeBlock.textContent; - let language = [...codeBlock.classList].filter(c => c.startsWith("language-"))[0].replace("language-", ""); - const editorId = ['javascript', 'python'].includes(language) ? 'script' : 'markup'; - const title = codeBlock.closest('.h3-wrap')?.querySelector('h3')?.textContent.replace('#', '') || ''; - - if (language === 'python') { - if (content.includes('>>> ')) { - content = content - .trim() - .split('\n') - .map(line => line.startsWith('>>> ') ? line.replace('>>> ', '') : `# ${line}`) - .join('\n'); - } - if (content.includes('from typing import')) { - language = 'python-wasm'; - } - } - // Add click event listener to run button runButton.addEventListener('click', () => { - const config = { + let content = codeBlock.textContent; + let language = [...codeBlock.classList].filter(c => c.startsWith("language-"))[0].replace("language-", ""); + const editorId = ['javascript', 'python'].includes(language) ? 'script' : 'markup'; + const title = codeBlock.closest('.h3-wrap')?.querySelector('h3')?.textContent.replace('#', '') || ''; + + if (language === 'python') { + if (content.includes('>>> ')) { + content = content + .trim() + .split('\n') + .map(line => line.startsWith('>>> ') ? line.replace('>>> ', '') : `# ${line}`) + .join('\n'); + } + if (content.includes('from typing import')) { + language = 'python-wasm'; + } + } + + let config = { title, activeEditor: editorId, [editorId]: { @@ -123,6 +123,23 @@ } } + if (language === 'markdown') { + config = { + ...config, + style: { + language: 'css', + content: '@import "github-markdown-css";\n\nbody {\n border: 1px solid #e1e4e8;\n border-radius: 4px;\n padding: 20px;\n margin: 20px !important;\n}\n', + }, + script: { + language: 'javascript', + content: 'document.body.classList.add("markdown-body");' + }, + tools: { + status: 'closed', + } + } + } + const plaugroundUrl = livecodes.getPlaygroundUrl({config}); window.open(plaugroundUrl, '_blank'); }); From e4c785d8ddb9041cbfb8f6a16c577fc08f9062c6 Mon Sep 17 00:00:00 2001 From: Hatem Hosny Date: Sun, 5 Jan 2025 02:03:11 +0200 Subject: [PATCH 3/6] feat: add support for running sass --- source/_posts/sass.md | 1 + themes/coo/layout/_partial/scripts.ejs | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/source/_posts/sass.md b/source/_posts/sass.md index 312c160..2ac1181 100644 --- a/source/_posts/sass.md +++ b/source/_posts/sass.md @@ -11,6 +11,7 @@ intro: | This is a quick reference cheat sheet that lists the most useful features of [SASS](https://sass-lang.com). plugins: - copyCode + - runCode --- ## Sass Basics diff --git a/themes/coo/layout/_partial/scripts.ejs b/themes/coo/layout/_partial/scripts.ejs index 49dcbb9..2342322 100644 --- a/themes/coo/layout/_partial/scripts.ejs +++ b/themes/coo/layout/_partial/scripts.ejs @@ -95,7 +95,11 @@ runButton.addEventListener('click', () => { let content = codeBlock.textContent; let language = [...codeBlock.classList].filter(c => c.startsWith("language-"))[0].replace("language-", ""); - const editorId = ['javascript', 'python'].includes(language) ? 'script' : 'markup'; + const editorId = ['javascript', 'python'].includes(language) + ? 'script' + : ['scss'].includes(language) + ? 'style' + : 'markup'; const title = codeBlock.closest('.h3-wrap')?.querySelector('h3')?.textContent.replace('#', '') || ''; if (language === 'python') { @@ -111,7 +115,7 @@ } } - let config = { + const baseConfig = { title, activeEditor: editorId, [editorId]: { @@ -122,10 +126,11 @@ status: 'open', } } + let config = baseConfig; if (language === 'markdown') { config = { - ...config, + ...baseConfig, style: { language: 'css', content: '@import "github-markdown-css";\n\nbody {\n border: 1px solid #e1e4e8;\n border-radius: 4px;\n padding: 20px;\n margin: 20px !important;\n}\n', @@ -140,6 +145,16 @@ } } + if (language === 'scss') { + config = { + ...baseConfig, + tools: { + status: 'open', + active: 'compiled', + } + } + } + const plaugroundUrl = livecodes.getPlaygroundUrl({config}); window.open(plaugroundUrl, '_blank'); }); From a35a51cc05f9a348d3da08314db16a82db42a522 Mon Sep 17 00:00:00 2001 From: Hatem Hosny Date: Sun, 5 Jan 2025 02:15:58 +0200 Subject: [PATCH 4/6] feat: add support for running jquery --- source/_posts/jquery.md | 5 +++-- themes/coo/layout/_partial/scripts.ejs | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/source/_posts/jquery.md b/source/_posts/jquery.md index f334b0c..0b6b957 100644 --- a/source/_posts/jquery.md +++ b/source/_posts/jquery.md @@ -14,19 +14,20 @@ intro: | plugins: - tooltip - copyCode + - runCode --- ## Getting Started ### Including jQuery -```javascript {.wrap} +```html {.wrap} ``` #### Official CDN -```javascript {.wrap} +```html {.wrap} ``` diff --git a/themes/coo/layout/_partial/scripts.ejs b/themes/coo/layout/_partial/scripts.ejs index 2342322..9fa81dc 100644 --- a/themes/coo/layout/_partial/scripts.ejs +++ b/themes/coo/layout/_partial/scripts.ejs @@ -155,6 +155,19 @@ } } + if (document.querySelector('h1').textContent.includes('jQuery')) { + config = { + ...baseConfig, + markup: { + language: 'html', + content: `