mirror of
https://github.com/Fechin/reference.git
synced 2026-11-21 13:34:06 +03:00
Merge pull request #628 from hatemhosny/run-code
Add support for running code
This commit is contained in:
7 files changed
+126
-7
No files matched your search
@@ -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
|
||||
|
||||
@@ -14,19 +14,20 @@ intro: |
|
||||
plugins:
|
||||
- tooltip
|
||||
- copyCode
|
||||
- runCode
|
||||
---
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Including jQuery
|
||||
|
||||
```javascript {.wrap}
|
||||
```html {.wrap}
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
|
||||
```
|
||||
|
||||
#### Official CDN
|
||||
|
||||
```javascript {.wrap}
|
||||
```html {.wrap}
|
||||
<script src="https://code.jquery.com/jquery-3.5.1.min.js" crossorigin="anonymous"></script>
|
||||
```
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ categories:
|
||||
intro: This is a quick reference cheat sheet to the Markdown syntax.
|
||||
plugins:
|
||||
- copyCode
|
||||
- runCode
|
||||
---
|
||||
|
||||
## Markdown Quick Reference
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -11,6 +11,7 @@ intro: |
|
||||
A TypeScript cheat sheet with the most important concepts, functions, methods, and more. A complete quick reference for beginners.
|
||||
plugins:
|
||||
- copyCode
|
||||
- runCode
|
||||
---
|
||||
|
||||
## Getting Started
|
||||
@@ -293,11 +294,7 @@ class Greeter {
|
||||
|
||||
```ts
|
||||
function enumerable(value: boolean) {
|
||||
return function (
|
||||
target: any,
|
||||
propertyKey: string,
|
||||
descriptor: PropertyDescriptor
|
||||
) {
|
||||
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
||||
descriptor.enumerable = value;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -72,3 +72,120 @@
|
||||
});
|
||||
</script>
|
||||
<% } %>
|
||||
|
||||
|
||||
<% if (is_post() && page.plugins != undefined && page.plugins.includes("runCode") ){ %>
|
||||
<script src="https://cdn.jsdelivr.net/npm/livecodes@0.7.2/livecodes.umd.js"></script>
|
||||
<script>
|
||||
// Get all pre > code elements
|
||||
const code_Blocks = document.querySelectorAll('pre > code');
|
||||
const runInnerHTML = '<svg height="1em" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g stroke-width="0"></g><g stroke-linecap="round" stroke-linejoin="round"></g><g><path d="M16.6582 9.28638C18.098 10.1862 18.8178 10.6361 19.0647 11.2122C19.2803 11.7152 19.2803 12.2847 19.0647 12.7878C18.8178 13.3638 18.098 13.8137 16.6582 14.7136L9.896 18.94C8.29805 19.9387 7.49907 20.4381 6.83973 20.385C6.26501 20.3388 5.73818 20.0469 5.3944 19.584C5 19.053 5 18.1108 5 16.2264V7.77357C5 5.88919 5 4.94701 5.3944 4.41598C5.73818 3.9531 6.26501 3.66111 6.83973 3.6149C7.49907 3.5619 8.29805 4.06126 9.896 5.05998L16.6582 9.28638Z" stroke="currentColor" stroke-width="2" stroke-linejoin="round"></path></g></svg>';
|
||||
code_Blocks.forEach(function (codeBlock) {
|
||||
let content = codeBlock.textContent;
|
||||
let language = [...codeBlock.classList].filter(c => c.startsWith("language-"))[0].replace("language-", "");
|
||||
|
||||
if (language === 'shell') return;
|
||||
|
||||
// Create a run button element
|
||||
const runButton = document.createElement('button');
|
||||
runButton.className = 'bg-emerald-500 p-2 text-white fadeIn shadow-xl rounded flex items-center absolute top-12 right-2';
|
||||
runButton.innerHTML = runInnerHTML;
|
||||
runButton.style.opacity = "0";
|
||||
const parent = codeBlock.parentNode;
|
||||
|
||||
parent.style = "position: relative;"
|
||||
parent.insertBefore(runButton, codeBlock);
|
||||
|
||||
// Add click event listener to run button
|
||||
runButton.addEventListener('click', () => {
|
||||
if (language === 'ts') {
|
||||
language = 'typescript';
|
||||
}
|
||||
|
||||
const editorId = ['javascript', 'typescript', 'python'].includes(language)
|
||||
? 'script'
|
||||
: ['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,
|
||||
[editorId]: {
|
||||
language,
|
||||
content,
|
||||
},
|
||||
tools: {
|
||||
status: 'open',
|
||||
}
|
||||
}
|
||||
let config = baseConfig;
|
||||
|
||||
if (language === 'markdown') {
|
||||
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',
|
||||
},
|
||||
script: {
|
||||
language: 'javascript',
|
||||
content: 'document.body.classList.add("markdown-body");'
|
||||
},
|
||||
tools: {
|
||||
status: 'open',
|
||||
active: 'compiled',
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (['scss', 'typescript'].includes(language)) {
|
||||
config = {
|
||||
...baseConfig,
|
||||
tools: {
|
||||
status: 'open',
|
||||
active: 'compiled',
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (document.querySelector('h1').textContent.includes('jQuery')) {
|
||||
config = {
|
||||
...baseConfig,
|
||||
markup: {
|
||||
language: 'html',
|
||||
content: `<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></scr` + 'ipt>\n',
|
||||
},
|
||||
tools: {
|
||||
status: 'closed',
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const plaugroundUrl = livecodes.getPlaygroundUrl({config});
|
||||
window.open(plaugroundUrl, '_blank');
|
||||
});
|
||||
|
||||
parent.addEventListener('mouseenter', () => {
|
||||
runButton.style.opacity = "1";
|
||||
});
|
||||
parent.addEventListener('mouseleave', () => {
|
||||
runButton.style.opacity = "0";
|
||||
});
|
||||
});
|
||||
</script>
|
||||
<% } %>
|
||||
Reference in new issue
Block a user