From 29f76214b4e8c4dbde501adb8bf43c3d25ab137c Mon Sep 17 00:00:00 2001 From: Hatem Hosny Date: Sun, 5 Jan 2025 13:31:16 +0200 Subject: [PATCH 01/13] feat: add support for running css --- source/_posts/css3.md | 65 ++++++++++++++------------ themes/coo/layout/_partial/scripts.ejs | 11 ++++- 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/source/_posts/css3.md b/source/_posts/css3.md index 9f70c4e..51236ad 100644 --- a/source/_posts/css3.md +++ b/source/_posts/css3.md @@ -12,6 +12,7 @@ intro: | This is a quick reference cheat sheet for CSS goodness, listing selector syntax, properties, units and other useful bits of information. plugins: - copyCode + - runCode --- ## Getting Started @@ -66,10 +67,14 @@ Overrides all previous styling rules. ### Selector ```css -h1 { } -#job-title { } -div.hero { } -div > p { } +h1 { +} +#job-title { +} +div.hero { +} +div > p { +} ``` See: [Selectors](#css-selectors) @@ -245,7 +250,10 @@ p:first-child { | `div,p` | All divs and paragraphs | | `#idname *` | All elements inside #idname | -See also: [Type](https://developer.mozilla.org/en-US/docs/Web/CSS/Type_selectors) / [Class](https://developer.mozilla.org/en-US/docs/Web/CSS/Class_selectors) / [ID](https://developer.mozilla.org/en-US/docs/Web/CSS/ID_selectors) / [Universal](https://developer.mozilla.org/en-US/docs/Web/CSS/Universal_selectors) selectors +See also: [Type](https://developer.mozilla.org/en-US/docs/Web/CSS/Type_selectors) / +[Class](https://developer.mozilla.org/en-US/docs/Web/CSS/Class_selectors) / +[ID](https://developer.mozilla.org/en-US/docs/Web/CSS/ID_selectors) / +[Universal](https://developer.mozilla.org/en-US/docs/Web/CSS/Universal_selectors) selectors ### Combinators @@ -258,7 +266,9 @@ See also: [Type](https://developer.mozilla.org/en-US/docs/Web/CSS/Type_selectors | `div + p` | P tags immediately after div | | `div ~ p` | P tags preceded by div | -See also: [Adjacent](https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_combinator) / [Sibling](https://developer.mozilla.org/en-US/docs/Web/CSS/General_sibling_combinator) / [Child](https://developer.mozilla.org/en-US/docs/Web/CSS/Child_combinator) selectors +See also: [Adjacent](https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_combinator) / +[Sibling](https://developer.mozilla.org/en-US/docs/Web/CSS/General_sibling_combinator) / +[Child](https://developer.mozilla.org/en-US/docs/Web/CSS/Child_combinator) selectors ### Attribute selectors @@ -345,12 +355,10 @@ See also: [Attribute selectors](https://developer.mozilla.org/en-US/docs/Web/CSS | `letter-spacing:` | \ | | `line-height:` | \ | -| `font-weight:` | \ / bold / normal | -| `font-style:` | italic / normal | -| `text-decoration:` | underline / none | +| `font-weight:` | \ / bold / normal | | `font-style:` | italic / normal | | `text-decoration:` | underline / +none | -| `text-align:` | left / right
center / justify | -| `text-transform:` | capitalize / uppercase / lowercase | +| `text-align:` | left / right
center / justify | | `text-transform:` | capitalize / uppercase / lowercase | {.left-text} See also: [Font](https://developer.mozilla.org/en-US/docs/Web/CSS/font) @@ -453,14 +461,10 @@ color: currentcolor; /* keyword */ | ------------- | ------------- | | `background:` | _(Shorthand)_ | -| `background-color:` | See: [Colors](#css-colors) | -| `background-image:` | url(...) | -| `background-position:` | left/center/right
top/center/bottom | -| `background-size:` | cover X Y | -| `background-clip:` | border-box
padding-box
content-box | -| `background-repeat:` | no-repeat
repeat-x
repeat-y | -| `background-attachment:` | scroll/fixed/local | -{.left-text} +| `background-color:` | See: [Colors](#css-colors) | | `background-image:` | url(...) | | `background-position:` | +left/center/right
top/center/bottom | | `background-size:` | cover X Y | | `background-clip:` | +border-box
padding-box
content-box | | `background-repeat:` | no-repeat
repeat-x
repeat-y | | +`background-attachment:` | scroll/fixed/local | {.left-text} ### Shorthand {.secondary .col-span-2} @@ -475,16 +479,12 @@ color: currentcolor; /* keyword */ ```css {.wrap} background: url(img_man.jpg) no-repeat center; -background: url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top - repeat; +background: + url(img_flwr.gif) right bottom no-repeat, + url(paper.gif) left top repeat; background: rgb(2, 0, 36); -background: linear-gradient( - 90deg, - rgba(2, 0, 36, 1) 0%, - rgba(13, 232, 230, 1) 35%, - rgba(0, 212, 255, 1) 100% -); +background: linear-gradient(90deg, rgba(2, 0, 36, 1) 0%, rgba(13, 232, 230, 1) 35%, rgba(0, 212, 255, 1) 100%); ``` ## CSS The Box Model @@ -498,7 +498,10 @@ background: linear-gradient( } ``` -See also: [max-width](https://developer.mozilla.org/en-US/docs/Web/CSS/max-width) / [min-width](https://developer.mozilla.org/en-US/docs/Web/CSS/min-width) / [max-height](https://developer.mozilla.org/en-US/docs/Web/CSS/max-height) / [min-height](https://developer.mozilla.org/en-US/docs/Web/CSS/min-height) +See also: [max-width](https://developer.mozilla.org/en-US/docs/Web/CSS/max-width) / +[min-width](https://developer.mozilla.org/en-US/docs/Web/CSS/min-width) / +[max-height](https://developer.mozilla.org/en-US/docs/Web/CSS/max-height) / +[min-height](https://developer.mozilla.org/en-US/docs/Web/CSS/min-height) ### Margin / Padding @@ -509,7 +512,8 @@ See also: [max-width](https://developer.mozilla.org/en-US/docs/Web/CSS/max-width } ``` -See also: [Margin](https://developer.mozilla.org/en-US/docs/Web/CSS/margin) / [Padding](https://developer.mozilla.org/en-US/docs/Web/CSS/padding) +See also: [Margin](https://developer.mozilla.org/en-US/docs/Web/CSS/margin) / +[Padding](https://developer.mozilla.org/en-US/docs/Web/CSS/padding) ### Box-sizing @@ -764,8 +768,7 @@ A fixed-height top bar and a dynamic-height content area. } ``` -This creates columns that have different widths, but size accordingly according -to the circumstances. +This creates columns that have different widths, but size accordingly according to the circumstances. ### Vertical diff --git a/themes/coo/layout/_partial/scripts.ejs b/themes/coo/layout/_partial/scripts.ejs index e5d09a9..31a7f8c 100644 --- a/themes/coo/layout/_partial/scripts.ejs +++ b/themes/coo/layout/_partial/scripts.ejs @@ -104,7 +104,7 @@ const editorId = ['javascript', 'typescript', 'python'].includes(language) ? 'script' - : ['scss'].includes(language) + : ['css', 'scss'].includes(language) ? 'style' : 'markup'; const title = codeBlock.closest('.h3-wrap')?.querySelector('h3')?.textContent.replace('#', '') || ''; @@ -153,6 +153,15 @@ } } + if (['html', 'css'].includes(language)) { + config = { + ...baseConfig, + tools: { + status: 'closed', + } + } + } + if (['scss', 'typescript'].includes(language)) { config = { ...baseConfig, From 9e2a20c3691d8de0cc7d5428efd92f8f3da2c075 Mon Sep 17 00:00:00 2001 From: Hatem Hosny Date: Sun, 5 Jan 2025 13:36:34 +0200 Subject: [PATCH 02/13] feat: add support for running html --- source/_posts/html.md | 1 + 1 file changed, 1 insertion(+) diff --git a/source/_posts/html.md b/source/_posts/html.md index 5d4d7bd..4a6fdd4 100644 --- a/source/_posts/html.md +++ b/source/_posts/html.md @@ -10,6 +10,7 @@ intro: | This HTML quick reference cheat sheet lists the common HTML and HTML5 tags in readable layout. plugins: - copyCode + - runCode --- ## Getting Started From 9609a67b1d4c96e54270a7eee58819703f6fa884 Mon Sep 17 00:00:00 2001 From: Hatem Hosny Date: Sun, 5 Jan 2025 14:30:48 +0200 Subject: [PATCH 03/13] feat: add support for running php --- source/_posts/php.md | 1 + themes/coo/layout/_partial/scripts.ejs | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/source/_posts/php.md b/source/_posts/php.md index a135035..6a8c890 100644 --- a/source/_posts/php.md +++ b/source/_posts/php.md @@ -10,6 +10,7 @@ intro: | This [PHP](https://www.php.net/manual/en/) cheat sheet provides a reference for quickly looking up the correct syntax for the code you use most frequently. plugins: - copyCode + - runCode --- ## Getting Started diff --git a/themes/coo/layout/_partial/scripts.ejs b/themes/coo/layout/_partial/scripts.ejs index 31a7f8c..59c627f 100644 --- a/themes/coo/layout/_partial/scripts.ejs +++ b/themes/coo/layout/_partial/scripts.ejs @@ -81,7 +81,7 @@ const code_Blocks = document.querySelectorAll('pre > code'); const runInnerHTML = ''; code_Blocks.forEach(function (codeBlock) { - let content = codeBlock.textContent; + let content = codeBlock.textContent || ''; let language = [...codeBlock.classList].filter(c => c.startsWith("language-"))[0].replace("language-", ""); if (language === 'shell') return; @@ -102,7 +102,7 @@ language = 'typescript'; } - const editorId = ['javascript', 'typescript', 'python'].includes(language) + const editorId = ['javascript', 'typescript', 'python', 'php'].includes(language) ? 'script' : ['css', 'scss'].includes(language) ? 'style' @@ -185,6 +185,23 @@ } } + if (['php'].includes(language)) { + config = { + ...baseConfig, + style: { + language: 'css', + content: 'body { white-space: pre-wrap; }\n', + }, + script: { + language: 'php-wasm', + content: !content.trim().startsWith('` : content, + }, + tools: { + status: 'closed', + } + } + } + const plaugroundUrl = livecodes.getPlaygroundUrl({config}); window.open(plaugroundUrl, '_blank'); }); From 5c28df96727e8cf589e4b62ff617554639fe5ac2 Mon Sep 17 00:00:00 2001 From: Hatem Hosny Date: Sun, 5 Jan 2025 14:34:25 +0200 Subject: [PATCH 04/13] feat: add support for running es6 --- source/_posts/es6.md | 1 + themes/coo/layout/_partial/scripts.ejs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/source/_posts/es6.md b/source/_posts/es6.md index 8139520..3b39a1f 100644 --- a/source/_posts/es6.md +++ b/source/_posts/es6.md @@ -11,6 +11,7 @@ intro: | A quick reference cheatsheet of what's new in JavaScript for ES2015, ES2016, ES2017, ES2018 and beyond plugins: - copyCode + - runCode --- ## Getting Started diff --git a/themes/coo/layout/_partial/scripts.ejs b/themes/coo/layout/_partial/scripts.ejs index 59c627f..0628c4b 100644 --- a/themes/coo/layout/_partial/scripts.ejs +++ b/themes/coo/layout/_partial/scripts.ejs @@ -102,7 +102,7 @@ language = 'typescript'; } - const editorId = ['javascript', 'typescript', 'python', 'php'].includes(language) + const editorId = ['js', 'javascript', 'typescript', 'python', 'php'].includes(language) ? 'script' : ['css', 'scss'].includes(language) ? 'style' From d87846732714880b81fcb9f67d50c1a48a0b8ab6 Mon Sep 17 00:00:00 2001 From: Hatem Hosny Date: Sun, 5 Jan 2025 14:57:12 +0200 Subject: [PATCH 05/13] feat: add support for running ejs --- source/_posts/ejs.md | 4 +++- themes/coo/layout/_partial/scripts.ejs | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/source/_posts/ejs.md b/source/_posts/ejs.md index 534a253..91dd1a9 100644 --- a/source/_posts/ejs.md +++ b/source/_posts/ejs.md @@ -12,6 +12,7 @@ intro: EJS(Embedded JavaScript) is a simple templating language that lets you generate HTML markup with plain JavaScript. plugins: - copyCode + - runCode --- @@ -58,10 +59,11 @@ Pass EJS a template string and some data. ### Browser Support ```html - + ``` diff --git a/themes/coo/layout/_partial/scripts.ejs b/themes/coo/layout/_partial/scripts.ejs index 0628c4b..e057c0c 100644 --- a/themes/coo/layout/_partial/scripts.ejs +++ b/themes/coo/layout/_partial/scripts.ejs @@ -185,6 +185,28 @@ } } + if (language === 'html' && document.querySelector('h1').textContent.includes('EJS')) { + config = { + ...baseConfig, + markup: { + language: 'ejs', + content, + }, + script: { + language: 'javascript', + content: "window.livecodes.templateData = {\n userLoggedIn: false,\n user: { username: 'geddy', email: 'geddy@example.com' },\n users: [\n { username: 'geddy', email: 'geddy@example.com' },\n { username: 'neil', email: 'neil@example.com' },\n { username: 'alex', email: 'alex@example.com' },\n ],\n};\n", + }, + tools: { + status: 'open', + }, + customSettings: { + "template": { + "prerender": false, + }, + }, + } + } + if (['php'].includes(language)) { config = { ...baseConfig, From 870fe5177b8c980bc07de4f2dd29bdcc871cd2da Mon Sep 17 00:00:00 2001 From: Hatem Hosny Date: Sun, 5 Jan 2025 17:12:31 +0200 Subject: [PATCH 06/13] feat: add support for running react --- source/_posts/react.md | 21 +++++++++++++-------- themes/coo/layout/_partial/scripts.ejs | 20 ++++++++++++++++++-- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/source/_posts/react.md b/source/_posts/react.md index c44a1c1..77c13e4 100644 --- a/source/_posts/react.md +++ b/source/_posts/react.md @@ -11,6 +11,7 @@ intro: | A React cheat sheet with the most important concepts, functions, methods, and more. A complete quick reference for beginners. plugins: - copyCode + - runCode --- ## Getting Started @@ -62,7 +63,7 @@ return ( ```javascript import React from "react"; -export default function Weather(props) { +function Weather(props) { if (props.temperature >= 20) { return (

@@ -77,6 +78,8 @@ export default function Weather(props) { ); } } + +export default () => ; ``` Note: A component must always return something. @@ -140,14 +143,14 @@ Note: External components are found on npmjs.com and need to be imported first. ```javascript import React from "react"; -export default function Hello(props) { +function Hello(props) { function fullName() { return `${props.firstName} ${props.lastName}`; } return

{fullName()}

; } -; +export default () => ; ``` ## Properties {.cols-2} @@ -539,6 +542,7 @@ Note: Custom Hooks are reusable functions in React that contain logic shared acr to extract stateful logic from components into standalone functions. ### Creating Refs in Class Components + ```javascript import React, { Component } from "react"; @@ -561,8 +565,8 @@ export default MyComponent; ``` ### Using Refs in Functional Components -```javascript +```javascript import React, { useRef, useEffect } from "react"; function MyComponent() { @@ -579,15 +583,15 @@ export default MyComponent; ``` ### Callback Refs -```javascript +```javascript import React, { Component } from "react"; class MyComponent extends Component { constructor(props) { super(props); this.myRef = null; - this.setRef = element => { + this.setRef = (element) => { this.myRef = element; }; } @@ -605,6 +609,7 @@ export default MyComponent; ``` ### Forwarding Refs + ```javascript Copy code import React from "react"; @@ -621,10 +626,10 @@ const ref = React.createRef(); ``` ### Accessing DOM Elements with Refs + ### Refs are often used to access and interact with DOM elements directly. Here's an example where we focus an input element using a ref: ```javascript - import React, { useRef, useEffect } from "react"; function FocusInput() { @@ -642,10 +647,10 @@ export default FocusInput; ``` ### Managing Focus with Refs + ### You can also manage focus between multiple elements using refs: ```javascript - import React, { useRef } from "react"; function Form() { diff --git a/themes/coo/layout/_partial/scripts.ejs b/themes/coo/layout/_partial/scripts.ejs index e057c0c..57a39f3 100644 --- a/themes/coo/layout/_partial/scripts.ejs +++ b/themes/coo/layout/_partial/scripts.ejs @@ -80,6 +80,7 @@ // Get all pre > code elements const code_Blocks = document.querySelectorAll('pre > code'); const runInnerHTML = ''; + const pageHeader = (document.querySelector('h1')?.textContent || '').trim(); code_Blocks.forEach(function (codeBlock) { let content = codeBlock.textContent || ''; let language = [...codeBlock.classList].filter(c => c.startsWith("language-"))[0].replace("language-", ""); @@ -172,7 +173,7 @@ } } - if (document.querySelector('h1').textContent.includes('jQuery')) { + if (pageHeader.startsWith('jQuery')) { config = { ...baseConfig, markup: { @@ -185,7 +186,7 @@ } } - if (language === 'html' && document.querySelector('h1').textContent.includes('EJS')) { + if (language === 'html' && pageHeader.startsWith('EJS')) { config = { ...baseConfig, markup: { @@ -207,6 +208,21 @@ } } + if (language === 'javascript' && pageHeader.startsWith('React')) { + config = { + ...baseConfig, + script: { + language: 'jsx', + content, + }, + customSettings: { + imports: { + 'axios': 'https://cdn.skypack.dev/axios', + }, + }, + } + } + if (['php'].includes(language)) { config = { ...baseConfig, From da53b553bab89b1bd90fcdd1e426270fc427a8d0 Mon Sep 17 00:00:00 2001 From: Hatem Hosny Date: Sun, 5 Jan 2025 17:17:56 +0200 Subject: [PATCH 07/13] feat: add support for running ruby --- source/_posts/ruby.md | 1 + themes/coo/layout/_partial/scripts.ejs | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/source/_posts/ruby.md b/source/_posts/ruby.md index eee2041..10803dc 100644 --- a/source/_posts/ruby.md +++ b/source/_posts/ruby.md @@ -11,6 +11,7 @@ intro: | The [Ruby](https://www.ruby-lang.org/) cheat sheet is a one-page reference sheet for the Ruby programming language. plugins: - copyCode + - runCode --- ## Getting Started diff --git a/themes/coo/layout/_partial/scripts.ejs b/themes/coo/layout/_partial/scripts.ejs index 57a39f3..ab1cd88 100644 --- a/themes/coo/layout/_partial/scripts.ejs +++ b/themes/coo/layout/_partial/scripts.ejs @@ -85,7 +85,7 @@ let content = codeBlock.textContent || ''; let language = [...codeBlock.classList].filter(c => c.startsWith("language-"))[0].replace("language-", ""); - if (language === 'shell') return; + if (['shell', 'bash'].includes(language)) return; // Create a run button element const runButton = document.createElement('button'); @@ -103,7 +103,7 @@ language = 'typescript'; } - const editorId = ['js', 'javascript', 'typescript', 'python', 'php'].includes(language) + const editorId = ['js', 'javascript', 'typescript', 'python', 'php', 'ruby'].includes(language) ? 'script' : ['css', 'scss'].includes(language) ? 'style' From 4c68afe0f915c7da6d3a4caa11dea96d970af6b9 Mon Sep 17 00:00:00 2001 From: Hatem Hosny Date: Sun, 5 Jan 2025 17:50:31 +0200 Subject: [PATCH 08/13] feat: add support for running perl --- source/_posts/perl.md | 5 ++++- themes/coo/layout/_partial/scripts.ejs | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/source/_posts/perl.md b/source/_posts/perl.md index 8d74397..8484d92 100644 --- a/source/_posts/perl.md +++ b/source/_posts/perl.md @@ -9,6 +9,7 @@ intro: | The perl quick reference cheat sheet that aims at providing help on writing basic syntax and methods. plugins: - copyCode + - runCode --- ## Getting Started @@ -36,7 +37,9 @@ $make install - Download either 32bit or 64bit version of installation. -- Run the downloaded file by double-clicking it in Windows Explorer. This brings up the Perl install wizard, which is really easy to use. Just accept the default settings, wait until the installation is finished, and you're ready to roll! +- Run the downloaded file by double-clicking it in Windows Explorer. This brings up the Perl install wizard, which is + really easy to use. Just accept the default settings, wait until the installation is finished, and you're ready to + roll! ### Macintosh Installation diff --git a/themes/coo/layout/_partial/scripts.ejs b/themes/coo/layout/_partial/scripts.ejs index ab1cd88..e317e88 100644 --- a/themes/coo/layout/_partial/scripts.ejs +++ b/themes/coo/layout/_partial/scripts.ejs @@ -103,7 +103,7 @@ language = 'typescript'; } - const editorId = ['js', 'javascript', 'typescript', 'python', 'php', 'ruby'].includes(language) + const editorId = ['js', 'javascript', 'typescript', 'python', 'php', 'ruby', 'perl'].includes(language) ? 'script' : ['css', 'scss'].includes(language) ? 'style' From d407354e16843ea04e3155fee6ecfb8afd03ca06 Mon Sep 17 00:00:00 2001 From: Hatem Hosny Date: Sun, 5 Jan 2025 18:01:48 +0200 Subject: [PATCH 09/13] feat: add support for running html canvas --- source/_posts/canvas.md | 8 ++------ themes/coo/layout/_partial/scripts.ejs | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/source/_posts/canvas.md b/source/_posts/canvas.md index 44bae10..15cc57a 100644 --- a/source/_posts/canvas.md +++ b/source/_posts/canvas.md @@ -10,6 +10,7 @@ intro: | This HTML Canvas quick reference cheat sheet lists the common HTML5 Canvas design tags in readable layout. plugins: - copyCode + - runCode --- ## Getting Started @@ -23,12 +24,7 @@ plugins: Canvas Example - + diff --git a/themes/coo/layout/_partial/scripts.ejs b/themes/coo/layout/_partial/scripts.ejs index e317e88..ba8239f 100644 --- a/themes/coo/layout/_partial/scripts.ejs +++ b/themes/coo/layout/_partial/scripts.ejs @@ -102,8 +102,11 @@ if (language === 'ts') { language = 'typescript'; } + if (language === 'js') { + language = 'javascript'; + } - const editorId = ['js', 'javascript', 'typescript', 'python', 'php', 'ruby', 'perl'].includes(language) + const editorId = ['javascript', 'typescript', 'python', 'php', 'ruby', 'perl'].includes(language) ? 'script' : ['css', 'scss'].includes(language) ? 'style' @@ -208,6 +211,26 @@ } } + if (language === 'javascript' && pageHeader.startsWith('HTML Canvas')) { + if (!content.includes('const ctx =')) { + content = `const canvas = document.getElementById("myCanvas");\nconst ctx = canvas.getContext("2d");\n\n${content}`; + } + config = { + ...baseConfig, + markup: { + language: 'html', + content: "\n", + }, + script: { + language, + content, + }, + tools: { + status: 'closed', + }, + } + } + if (language === 'javascript' && pageHeader.startsWith('React')) { config = { ...baseConfig, From fb1a5d9a3fed80652eb933152beb14f38c81b320 Mon Sep 17 00:00:00 2001 From: Hatem Hosny Date: Sun, 5 Jan 2025 18:46:19 +0200 Subject: [PATCH 10/13] feat: add support for running go --- source/_posts/go.md | 1 + themes/coo/layout/_partial/scripts.ejs | 62 +++++++++++++++++++------- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/source/_posts/go.md b/source/_posts/go.md index 1ec8555..d350639 100644 --- a/source/_posts/go.md +++ b/source/_posts/go.md @@ -10,6 +10,7 @@ intro: | This cheat sheet provided basic syntax and methods to help you using [Go](https://go.dev/). plugins: - copyCode + - runCode --- ## Getting Started diff --git a/themes/coo/layout/_partial/scripts.ejs b/themes/coo/layout/_partial/scripts.ejs index ba8239f..74dd96a 100644 --- a/themes/coo/layout/_partial/scripts.ejs +++ b/themes/coo/layout/_partial/scripts.ejs @@ -106,26 +106,13 @@ language = 'javascript'; } - const editorId = ['javascript', 'typescript', 'python', 'php', 'ruby', 'perl'].includes(language) + const editorId = ['javascript', 'typescript', 'python', 'php', 'ruby', 'go', 'perl'].includes(language) ? 'script' : ['css', 'scss'].includes(language) ? 'style' : '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'; - } - } - const baseConfig = { title, activeEditor: editorId, @@ -246,7 +233,7 @@ } } - if (['php'].includes(language)) { + if (language === 'php') { config = { ...baseConfig, style: { @@ -263,6 +250,51 @@ } } + 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'; + } + config = { + ...baseConfig, + script: { + language, + content, + }, + } + } + + if (language === 'go') { + if (!content.includes('func main() {') && !content.includes('import')) { + if (content.includes('func ')) { + const freeCode = content.split('\n').filter(line => !line.startsWith('func ') && !line.startsWith(' ') && !line.startsWith('}')).join('\n'); + content = `${content.replace(freeCode, '')}\nfunc main() {\n${freeCode.trim().split('\n').map(line => ` ${line}`).join('\n')}\n}`; + } else { + content = `func main() {\n${content.trim().split('\n').map(line => ` ${line}`).join('\n')}\n}`; + } + } + + if (content.includes('fmt.') && !content.includes('import')) { + content = `import "fmt"\n\n${content}` + } + if (!content.includes('package main')) { + content = `package main\n\n${content}` + } + config = { + ...baseConfig, + script: { + language, + content, + }, + } + } + const plaugroundUrl = livecodes.getPlaygroundUrl({config}); window.open(plaugroundUrl, '_blank'); }); From 2d35d05ec8bdf72ad3a828ed6d2ce7e4876f5754 Mon Sep 17 00:00:00 2001 From: Hatem Hosny Date: Sun, 5 Jan 2025 20:10:05 +0200 Subject: [PATCH 11/13] feat: add support for running cpp --- source/_posts/cpp.md | 2 ++ themes/coo/layout/_partial/scripts.ejs | 39 +++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/source/_posts/cpp.md b/source/_posts/cpp.md index 9c17778..2c01793 100644 --- a/source/_posts/cpp.md +++ b/source/_posts/cpp.md @@ -9,6 +9,7 @@ intro: | C++ quick reference cheat sheet that provides basic syntax and methods. plugins: - copyCode + - runCode --- ## Getting Started @@ -467,6 +468,7 @@ for(int i = 1; i > 0; i++) { ```cpp #include +#include int main() { diff --git a/themes/coo/layout/_partial/scripts.ejs b/themes/coo/layout/_partial/scripts.ejs index 74dd96a..1c4ae8b 100644 --- a/themes/coo/layout/_partial/scripts.ejs +++ b/themes/coo/layout/_partial/scripts.ejs @@ -83,9 +83,9 @@ const pageHeader = (document.querySelector('h1')?.textContent || '').trim(); code_Blocks.forEach(function (codeBlock) { let content = codeBlock.textContent || ''; - let language = [...codeBlock.classList].filter(c => c.startsWith("language-"))[0].replace("language-", ""); + let language = [...codeBlock.classList].find(c => c.startsWith("language-"))?.replace("language-", "") || ''; - if (['shell', 'bash'].includes(language)) return; + if (['', 'shell', 'bash'].includes(language)) return; // Create a run button element const runButton = document.createElement('button'); @@ -106,7 +106,7 @@ language = 'javascript'; } - const editorId = ['javascript', 'typescript', 'python', 'php', 'ruby', 'go', 'perl'].includes(language) + const editorId = ['javascript', 'typescript', 'python', 'php', 'ruby', 'go', 'perl', 'cpp'].includes(language) ? 'script' : ['css', 'scss'].includes(language) ? 'style' @@ -279,7 +279,6 @@ content = `func main() {\n${content.trim().split('\n').map(line => ` ${line}`).join('\n')}\n}`; } } - if (content.includes('fmt.') && !content.includes('import')) { content = `import "fmt"\n\n${content}` } @@ -295,6 +294,38 @@ } } + if (language === 'cpp') { + if (!content.includes('int main()')) { + if (content.includes('class ')) { + const freeCode = content.split('\n').filter(line => !line.startsWith('class ') && !line.startsWith(' ') && !line.startsWith('}')).join('\n'); + content = `${content.replace(freeCode, '')}\nint main()\n{\n${freeCode.trim().split('\n').map(line => ` ${line}`).join('\n')}\n}`; + } else { + content = `int main()\n{\n${content.trim().split('\n').map(line => ` ${line}`).join('\n')}\n\n return 0;\n}\n`; + } + } + if (content.includes('std::') && !content.includes('#include')) { + content = '\n' + content; + if (content.includes('std::array') && !content.includes('#include ')) { + content = `#include \n${content}`; + } + if (content.includes('std::string') && !content.includes('#include ')) { + content = `#include \n${content}`; + } + if (content.includes('std::cout') && !content.includes('#include ')) { + content = `#include \n${content}`; + } + } else if (content.includes('cout') && !content.includes('#include ')) { + content = `#include \nusing namespace std;\n\n${content}`; + } + config = { + ...baseConfig, + script: { + language: 'cpp-wasm', + content, + }, + } + } + const plaugroundUrl = livecodes.getPlaygroundUrl({config}); window.open(plaugroundUrl, '_blank'); }); From 4a2860d724ab14528f5bb24ad333303240038d0b Mon Sep 17 00:00:00 2001 From: Hatem Hosny Date: Sun, 5 Jan 2025 20:54:30 +0200 Subject: [PATCH 12/13] feat: add support for running c --- source/_posts/c.md | 3 +- themes/coo/layout/_partial/scripts.ejs | 40 ++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/source/_posts/c.md b/source/_posts/c.md index cbd23a5..aaa744c 100644 --- a/source/_posts/c.md +++ b/source/_posts/c.md @@ -9,6 +9,7 @@ intro: | C quick reference cheat sheet that provides basic syntax and methods. plugins: - copyCode + - runCode --- ## Getting Started @@ -58,7 +59,7 @@ int y = 6; int sum = x + y; // add variables to sum // declare multiple variables -int x = 5, y = 6, z = 50; +int a = 5, b = 6, c = 50; ``` ### Constants diff --git a/themes/coo/layout/_partial/scripts.ejs b/themes/coo/layout/_partial/scripts.ejs index 1c4ae8b..69358e1 100644 --- a/themes/coo/layout/_partial/scripts.ejs +++ b/themes/coo/layout/_partial/scripts.ejs @@ -86,6 +86,10 @@ let language = [...codeBlock.classList].find(c => c.startsWith("language-"))?.replace("language-", "") || ''; if (['', 'shell', 'bash'].includes(language)) return; + if (language === 'c' && content.includes('FILE ')) { + // file operations are not supported in the online playground + return; + } // Create a run button element const runButton = document.createElement('button'); @@ -106,7 +110,7 @@ language = 'javascript'; } - const editorId = ['javascript', 'typescript', 'python', 'php', 'ruby', 'go', 'perl', 'cpp'].includes(language) + const editorId = ['javascript', 'typescript', 'python', 'php', 'ruby', 'go', 'perl', 'cpp', 'c'].includes(language) ? 'script' : ['css', 'scss'].includes(language) ? 'style' @@ -267,6 +271,9 @@ language, content, }, + tools: { + status: 'full', + } } } @@ -291,6 +298,9 @@ language, content, }, + tools: { + status: 'full', + } } } @@ -300,7 +310,7 @@ const freeCode = content.split('\n').filter(line => !line.startsWith('class ') && !line.startsWith(' ') && !line.startsWith('}')).join('\n'); content = `${content.replace(freeCode, '')}\nint main()\n{\n${freeCode.trim().split('\n').map(line => ` ${line}`).join('\n')}\n}`; } else { - content = `int main()\n{\n${content.trim().split('\n').map(line => ` ${line}`).join('\n')}\n\n return 0;\n}\n`; + content = `int main()\n{\n${content.trim().split('\n').map(line => ` ${line}`).join('\n')}\n\n return 0;\n}\n`; } } if (content.includes('std::') && !content.includes('#include')) { @@ -323,6 +333,32 @@ language: 'cpp-wasm', content, }, + tools: { + status: 'full', + } + } + } + + if (language === 'c') { + if (!content.includes('int main(') && !content.includes('void main(')) { + content = `int main(void)\n{\n${content.trim().split('\n').map(line => ` ${line}`).join('\n')}\n\n return 0;\n}\n`; + } + if ((content.includes('printf') || content.includes('scanf')) && !content.includes('#include ')) { + content = `#include \n\n${content}`; + } + if (content.includes('strcpy') && !content.includes('#include ')) { + content = `#include \n${content}`; + } + config = { + ...baseConfig, + script: { + language: 'cpp-wasm', + title: 'C (Wasm)', + content, + }, + tools: { + status: 'full', + } } } From 624c3042327d6339cc5791dd8e54a181342bd7ac Mon Sep 17 00:00:00 2001 From: Hatem Hosny Date: Sun, 5 Jan 2025 21:09:58 +0200 Subject: [PATCH 13/13] add links to LiveCodes docs and use permanent URL --- themes/coo/layout/_partial/scripts.ejs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/themes/coo/layout/_partial/scripts.ejs b/themes/coo/layout/_partial/scripts.ejs index 69358e1..8cd3882 100644 --- a/themes/coo/layout/_partial/scripts.ejs +++ b/themes/coo/layout/_partial/scripts.ejs @@ -75,6 +75,7 @@ <% if (is_post() && page.plugins != undefined && page.plugins.includes("runCode") ){ %> +