diff --git a/mu-plugins/helpers/helpers.php b/mu-plugins/helpers/helpers.php index 78e8d4fc..91c06dbf 100644 --- a/mu-plugins/helpers/helpers.php +++ b/mu-plugins/helpers/helpers.php @@ -1,6 +1,7 @@ 3; + + // Shell is flagged with `sh` or `bash` in the handbooks, but Prism uses `shell`. + if ( 'sh' === $lang || 'bash' === $lang ) { + $lang = 'shell'; + } + + return do_blocks( + sprintf( + '
%2$s',
+ $lang,
+ $content,
+ $show_line_numbers ? 'true' : 'false',
+ $show_line_numbers ? 'line-numbers' : ''
+ )
+ );
+}
+
+/**
+ * Trim off any extra space, including initial new lines.
+ * Strip out added by WordPress.
+ *
+ * @param string $content Shortcode content.
+ * @return string
+ */
+function _trim_code( $content ) {
+ $content = preg_replace( '/
/', '', $content );
+ $content = preg_replace( '/<\/p>\s*
/', "\n\n", $content ); + // Trim everything except leading spaces. + $content = trim( $content, "\n\r\t\v\x00" ); + return $content; +} diff --git a/mu-plugins/plugin-tweaks/code-syntax-block/prism.css b/mu-plugins/plugin-tweaks/code-syntax-block/prism.css new file mode 100644 index 00000000..96445c8c --- /dev/null +++ b/mu-plugins/plugin-tweaks/code-syntax-block/prism.css @@ -0,0 +1,246 @@ +/** + * Custom theme for developer.wordpress.org. + * Forked from a11y-dark theme by ericwbailey, which was based on the okaidia theme. + * + * https://github.com/PrismJS/prism-themes/blob/master/themes/prism-a11y-dark.css + * https://github.com/PrismJS/prism/blob/gh-pages/themes/prism-okaidia.css + */ + +code[class*="language-"], +pre[class*="language-"] { + color: #101517; + background: none; + font-family: Hack, "Fira Code", Consolas, Menlo, Monaco, "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Liberation Mono", "Nimbus Mono L", "Courier New", Courier, monospace; + text-align: left; + white-space: pre; + word-spacing: normal; + word-break: normal; + word-wrap: normal; + line-height: 1.5; + -moz-tab-size: 4; + -o-tab-size: 4; + tab-size: 4; + -webkit-hyphens: none; + -moz-hyphens: none; + -ms-hyphens: none; + hyphens: none; + transition: height 500ms; +} + +/* Code blocks */ +pre[class*="language-"] { + padding: 1em; + overflow: auto; + border-radius: 0.3em; +} + +:not(pre) > code[class*="language-"], +pre[class*="language-"] { + background: #f6f7f7; +} + +/* Inline code */ +:not(pre) > code[class*="language-"] { + padding: 0.1em; + border-radius: 0.3em; + white-space: normal; +} + +.token.comment, +.token.prolog, +.token.doctype, +.token.cdata { + color: #007017; +} + +.token.punctuation { + color: #50575e; +} + +.token.property, +.token.tag, +.token.constant, +.token.symbol, +.token.deleted { + color: #043959; +} + +.token.boolean, +.token.number { + color: #101517; +} + +.token.selector, +.token.attr-name, +.token.string, +.token.char, +.token.builtin, +.token.inserted { + color: #2271b1; +} + +.token.operator, +.token.entity, +.token.url, +.language-css .token.string, +.style .token.string, +.token.variable { + color: #a85f00; +} + +.token.function { + color: #b8236d; +} + +.token.keyword { + color: #135e96; + font-weight: 600; +} + +.token.atrule, +.token.attr-value, +.token.function-definition { + color: #101517; +} + +.token.important, +.token.bold { + font-weight: 700; +} + +.token.italic { + font-style: italic; +} + +.token.entity { + cursor: help; +} + +@media screen and (-ms-high-contrast: active) { + code[class*="language-"], + pre[class*="language-"] { + color: windowText; + background: window; + } + + :not(pre) > code[class*="language-"], + pre[class*="language-"] { + background: window; + } + + .token.important { + background: highlight; + color: window; + font-weight: 700; + } + + .token.atrule, + .token.attr-value, + .token.function, + .token.keyword, + .token.operator, + .token.selector { + font-weight: 700; + } + + .token.attr-value, + .token.comment, + .token.doctype, + .token.function, + .token.keyword, + .token.operator, + .token.property, + .token.string { + color: highlight; + } + + .token.attr-value, + .token.url { + font-weight: 700; + } +} + +/* Line Numbers */ +pre.line-numbers { + position: relative; + padding-left: 3.8em; + counter-reset: linenumber; +} + +pre.line-numbers > code { + position: relative; +} + +.line-numbers .line-numbers-rows { + position: absolute; + pointer-events: none; + top: 0; + font-size: 100%; + left: -3.8em; + width: 4em; /* works for line-numbers below 10000 lines */ + letter-spacing: -1px; + border-right: 0; + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; +} + +.line-numbers-rows > span { + pointer-events: none; + display: block; + counter-increment: linenumber; +} + +.line-numbers-rows > span::before { + content: counter(linenumber); + color: #5c6370; + display: block; + padding-right: 0.8em; + text-align: right; +} + +/* Copy button */ +.wp-code-block-button-container { + position: sticky; + top: calc(var(--wp-global-header-height) + var(--wp-admin--admin-bar--height)); + z-index: 1; + display: flex; + justify-content: right; + padding: 1rem; + background: #fff; + border-width: 1px 1px 0; + border-style: solid; + border-color: #dcdcde; + border-top-left-radius: 0.3em; + border-top-right-radius: 0.3em; +} + +@media screen and (max-width: 889px) { + .wp-code-block-button-container { + top: var(--wp-admin--admin-bar--height); + } +} + +@media screen and (max-width: 600px) { + .wp-code-block-button-container { + top: 0; + } +} + +.wp-code-block-button-container button { + font-size: 1.2rem !important; +} + +.wp-code-block-button-container button + button { + margin-left: 0.5em; +} + +.wp-code-block-button-container + pre { + margin-top: 0; + border-width: 0 1px 1px; + border-style: solid; + border-color: #dcdcde; + border-top-left-radius: 0; + border-top-right-radius: 0; +} diff --git a/mu-plugins/plugin-tweaks/index.php b/mu-plugins/plugin-tweaks/index.php index fd0907d9..61291415 100644 --- a/mu-plugins/plugin-tweaks/index.php +++ b/mu-plugins/plugin-tweaks/index.php @@ -9,5 +9,6 @@ defined( 'WPINC' ) || die(); -require_once __DIR__ . '/wporg-internal-notes.php'; +require_once __DIR__ . '/code-syntax-block/index.php'; require_once __DIR__ . '/stream.php'; +require_once __DIR__ . '/wporg-internal-notes.php';