Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and improve Prism code highlighting #886

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
{
"languages": ["markup", "css", "clike", "javascript"],
"plugins": ["line-numbers"],
"theme": "coy",
"css": true
}
]
Expand Down
5 changes: 4 additions & 1 deletion editor/css/editable-css.css
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@
background-color: var(--background-secondary);
}

.live .example-choice code {
.live .example-choice pre {
width: 100%;
margin: 0;
padding: 10px 0;
overflow-x: scroll;
}

.example-choice-list .example-choice:first-child {
Expand Down
38 changes: 30 additions & 8 deletions editor/css/editor-libs/prism-override.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,43 @@ code[class*="language-"] {
color: #333;
}

/* overrides for increased a11y */
.token {
color: var(--code-token-default);
}

.token.punctuation {
color: var(--code-token-punctuation);
}

.token.comment,
.token.block-comment,
.token.prolog,
.token.doctype,
.token.cdata {
color: #58636d;
color: var(--code-token-comment);
}

.token.keyword,
.token.tag .token.builtin {
color: var(--code-token-tag);
}

.token.attribute,
.token.attr-name {
color: var(--code-token-attribute-name);
}

.token.selector,
.token.attr-name,
.token.string,
.token.char,
.token.function,
.token.builtin,
.token.inserted {
color: #2545a7;
.token.number,
.token.atom {
color: var(--code-token-attribute-value);
}

.token.selector {
color: var(--code-token-selector);
}

.token.property {
color: var(--code-token-attribute-name);
}
1 change: 0 additions & 1 deletion editor/js/css-examples-libs.js

This file was deleted.

21 changes: 13 additions & 8 deletions editor/js/editable-css.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as clippy from "./editor-libs/clippy.js";
import * as mceEvents from "./editor-libs/events.js";
import highlightCode from "./editor-libs/highlight.js";
import * as mceUtils from "./editor-libs/mce-utils.js";

(function () {
Expand All @@ -23,6 +24,7 @@ import * as mceUtils from "./editor-libs/mce-utils.js";
var exampleChoice = exampleChoices[i];
var choiceButton = document.createElement("button");
var choiceButtonText = document.createElement("span");
var choiceCode = exampleChoice.querySelector("code");

choiceButton.setAttribute("type", "button");
choiceButton.classList.add("example-choice-button");
Expand All @@ -32,11 +34,16 @@ import * as mceUtils from "./editor-libs/mce-utils.js";
choiceButton.append(choiceButtonText);
exampleChoice.append(choiceButton);

originalChoices.push(exampleChoice.querySelector("code").textContent);
originalChoices.push(choiceCode.textContent);

if (exampleChoice.getAttribute("initial-choice")) {
initialChoice = indexOf(exampleChoices, exampleChoice);
}

// Re-highlight after editing code
choiceCode.addEventListener("focusout", function (e) {
highlightCode(e.target, "css");
});
}

mceEvents.register();
Expand All @@ -55,14 +62,12 @@ import * as mceUtils from "./editor-libs/mce-utils.js";

resetButton.addEventListener("click", function () {
for (var i = 0, l = exampleChoices.length; i < l; i++) {
var highlighted = Prism.highlight(
originalChoices[i],
Prism.languages.css
highlightCode(
exampleChoices[i].querySelector("code"),
"css",
originalChoices[i]
);
// IE11 does not support multiple selectors in `remove`
exampleChoices[i].classList.remove("invalid");
exampleChoices[i].classList.remove("selected");
exampleChoices[i].querySelector("code").innerHTML = highlighted;
exampleChoices[i].classList.remove("invalid", "selected");
}

// if there is an initial choice set, set it as selected
Expand Down
14 changes: 14 additions & 0 deletions editor/js/editor-libs/highlight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Prism from "prismjs";

/**
* Highlights codeblocks of specified code (or target's content) and applies it to the target element
*/
function highlightCode(target, language, code) {
var highlighted = Prism.highlight(
code || target.textContent,
Prism.languages[language]
);
target.innerHTML = highlighted;
}

export default highlightCode;
5 changes: 0 additions & 5 deletions editor/tmpl/live-css-tmpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<title>%title%</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
rel="stylesheet"
href="../../css/css-examples-libs.css%cache-buster%"
/>
<link rel="stylesheet" href="../../css/editor-css.css%cache-buster%" />
%example-css-src%
</head>
Expand All @@ -33,7 +29,6 @@ <h4 class="output-heading">%title%</h4>
</div>
</section>
<div id="user-message" class="user-message" aria-hidden="true">Copied!</div>
<script src="../../js/css-examples-libs.js%cache-buster%"></script>
<script src="../../js/editor-css.js%cache-buster%"></script>
%example-js-src%
</body>
Expand Down
8 changes: 0 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
"path": "./docs/css/codemirror-*.css",
"maxSize": "3 kB"
},
{
"path": "./docs/css/css-examples-libs-*.css",
"maxSize": "4 kB"
},
{
"path": "./docs/css/editor-css.css",
"maxSize": "4 kB"
Expand All @@ -45,10 +41,6 @@
"path": "./docs/js/codemirror-*.js",
"maxSize": "75 kB"
},
{
"path": "./docs/js/css-examples-libs-*.js",
"maxSize": "8 kB"
},
{
"path": "./docs/js/editor-css.js",
"maxSize": "4 kB"
Expand Down
1 change: 0 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export default {
"editor-css": "./editor/js/editable-css.js",
"editor-js": "./editor/js/editable-js.js",
"editor-wat": "./editor/js/editable-wat.js",
"css-examples-libs": "./editor/js/css-examples-libs.js",
},
output: {
path: fileURLToPath(new URL("docs/js", import.meta.url)),
Expand Down