Skip to content

Commit

Permalink
chore: update code blocks KTL-1707
Browse files Browse the repository at this point in the history
  • Loading branch information
nikpachoo committed Mar 4, 2025
1 parent 01462b9 commit 1664937
Show file tree
Hide file tree
Showing 35 changed files with 631 additions and 694 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,90 @@
package org.jetbrains.dokka.uitest.markdown

/**
* Contains examples of Markdown code showcasing Kotlin syntax highlighting.
*
* Contains examples of Markdown code.
*
* Here's a code block:
* Here's a code block with various Kotlin features to test syntax highlighting:
*
* ```
* class MyUIClass {
* val scope = MainScope() // the scope of MyUIClass, uses Dispatchers.Main
* // Single-line comment token
* /* Multi-line comment token */
* /** Documentation comment token */
*
* // Package declaration to test namespace token
* package com.example.highlighting
*
* // Imports to test namespace tokens
* import kotlin.random.Random
* import kotlin.collections.List
*
* // Type alias to test symbol token
* typealias Handler<T> = (T) -> Unit
* typealias AsyncOperation = suspend () -> Unit
*
* // Sealed interface to test class-name and keyword tokens
* sealed interface State {
* object Loading : State
* data class Success(val data: String) : State
* data class Error(val message: String) : State
* }
*
* // Class with various token types
* class SyntaxDemo {
* // Properties to test property and symbol tokens
* private val number: Int = 42 // number token
* protected var text: String = "Hello" // string token
* internal const val PI = 3.14159 // number token
*
* fun destroy() { // destroys an instance of MyUIClass
* scope.cancel() // cancels all coroutines launched in this scope
* // ... do the rest of cleanup here ...
* }
* protected var url: String = "https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/min.html"
*
* /*
* * Note: if this instance is destroyed or any of the launched coroutines
* * in this method throws an exception, then all nested coroutines are cancelled.
* */
* fun showSomeData() = scope.launch { // launched in the main thread
* // ... here we can use suspending functions or coroutine builders with other dispatchers
* draw(data) // draw in the main thread
* var pattern = Regex("""\b\d{3}-\d{3}-\d{4}\b""")
*
* // Companion with property tokens
* companion object {
* const val DEBUG = true // boolean token
* const val CHAR_SAMPLE = 'A' // char token
* @JvmField val EMPTY = "" // string token
* }
*
* // Function to test various tokens
* fun calculate(x: Double): Double {
* val multiplier = 2.5 // number token
* val enabled: Boolean = false // boolean token
*
* // Operators test
* val result = when {
* x <= 0 -> x * multiplier
* x >= 100 -> x / multiplier
* else -> x + multiplier
* }
*
* // Built-in types and functions test
* val numbers: List<Int> = listOf(1, 2, 3)
* val filtered = numbers
* .filter { it > 0 } // lambda and operator tokens
* .map { it.toString() } // function token
* .joinToString(separator = ", ")
*
* // String template and escape sequence tokens
* println("Result: $result\nFiltered: $filtered")
*
* return result
* }
*
* // Extension function with operator and symbol tokens
* infix fun Int.power(exponent: Int): Int {
* require(exponent >= 0) { "Exponent must be non-negative" }
* return when (exponent) {
* 0 -> 1
* 1 -> this
* else -> this * (this power (exponent - 1))
* }
* }
* }
* ```
*
* Here's inline code: `this` and `that` and `fun foo()` and `class Omg : MyInterface`
* Here's inline code with various token types:
* `val x: Int = 0`, `fun interface EventHandler`, `object : Runnable`,
* `class Sample<T : Any>`, `@Deprecated fun old()`, `var name: String?`
*/
class MarkdownCode {}
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ public enum class TokenStyle : Style {
public enum class TextStyle : Style {
Bold, Italic, Strong, Strikethrough, Paragraph,
Block, Span, Monospace, Indented, Cover, UnderCoverText, BreakableAfter, Breakable, InlineComment, Quotation,
FloatingRight, Var, Underlined
SourceLink, Var, Underlined
}

public enum class ContentStyle : Style {
Expand Down
12 changes: 12 additions & 0 deletions dokka-subprojects/plugin-base-frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions dokka-subprojects/plugin-base-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"dependencies": {
"@babel/core": "^7.16.0",
"@babel/preset-env": "^7.24.5",
"@fontsource/inter": "^5.1.1",
"@fontsource/jetbrains-mono": "^5.1.2",
"@jetbrains/babel-preset-jetbrains": "^2.3.1",
"@jetbrains/logos": "1.4.27",
"@jetbrains/ring-ui": "^5.1.28",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
--color-background-page-dt: rgb(38, 38, 40);
--color-background-footer: rgb(235, 235, 235);
--color-background-footer-dt: rgb(50, 50, 55);
--color-background-code: rgba(230, 230, 230, 1); //#E6E6E6

// Text colors (headings, paragraphs, labels)
--color-text: rgb(0, 0, 0);
Expand All @@ -36,21 +35,16 @@
--color-b70: rgba(0, 0, 0, 0.7);

// Code area colors
--color-cd-punctuation: rgb(153, 153, 153); // #999999;
--color-cd-keyword: rgb(0, 51, 179); // #0033B3;
--color-cd-keyword-alternative: rgba(204, 120, 50); // #CC7832;
--color-cd-builtin: rgb(6, 125, 23); // #067D17;
--color-cd-builtin-alternative: rgb(231, 191, 106); // #E8BF6A;
--color-cd-function: rgb(0, 98, 122); // #00627A;
--color-cd-function-alternative: rgb(255, 198, 109); // #FFC66D;
--color-cd-operator: rgb(154, 110, 58); // #9A6E3A;
--color-cd-operator-alternative: rgb(169, 183, 198); // #A9B7C6;
--color-cd-body: rgb(0, 0, 0); // #000000;
--color-cd-body-alternative: rgb(169, 183, 198); // #A9B7C6;
--color-background-inline-code: var(--color-b08);
--color-background-code-block: rgba(25, 25, 28, 0.05);

// Targets and source sets
--color-generic: rgb(83, 157, 243);
--color-jvm: rgb(77, 187, 95);
--color-js: rgb(255, 199, 0);
--color-wasm: rgb(255, 255, 255);

// Copy icon colors
--copy-icon-color: var(--color-b50);
--copy-icon-hover-color: var(--color-b70);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

:root {
--font-family-default: JetBrains Sans, Inter, system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
--font-family-default: Inter, system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
Ubuntu, Cantarell, Droid Sans, Helvetica Neue, Arial, sans-serif;
--font-family-mono: JetBrains Mono, SFMono-Regular, Consolas, Liberation Mono, Menlo, Courier, monospace;
--font-h1: 600 44px/44px var(--font-family-default);
Expand All @@ -12,5 +12,5 @@
--font-h4: 600 16px/24px var(--font-family-default);
--font-text-m: 400 16px/24px var(--font-family-default);
--font-text-s: 400 14px/20px var(--font-family-default);
--font-code: 400 16px/24px var(--font-family-mono);
--font-code: 400 15.5px/24px var(--font-family-mono);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2014-2025 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
import './styles.scss';

// helps with some corner cases where <wbr> starts working already,
// but the signature is not yet long enough to be wrapped

const CODE_BLOCK_PADDING = 16 * 2;

const symbolsObserver = new ResizeObserver((entries) => entries.forEach(wrapSymbolParameters));

function initHandlers() {
document.querySelectorAll('div.symbol').forEach((symbol) => symbolsObserver.observe(symbol));
}

if (document.readyState === 'loading') {
window.addEventListener('DOMContentLoaded', initHandlers);
} else {
initHandlers();
}

function createNbspIndent() {
const indent = document.createElement('span');
indent.append(document.createTextNode('\u00A0\u00A0\u00A0\u00A0'));
indent.classList.add('nbsp-indent');
return indent;
}

function wrapSymbolParameters(entry: ResizeObserverEntry) {
const symbol = entry.target;
const symbolBlockWidth = entry.borderBoxSize && entry.borderBoxSize[0] && entry.borderBoxSize[0].inlineSize;
const sourceButtonWidth = symbol.querySelector('[data-element-type="source-link"]')?.getBoundingClientRect().width || 0;

// Even though the script is marked as `defer` and we wait for `DOMContentLoaded` event,
// or if this block is a part of hidden tab, it can happen that `symbolBlockWidth` is 0,
// indicating that something hasn't been loaded.
// In this case, observer will be triggered onсe again when it will be ready.
if (symbolBlockWidth > 0) {
const node = symbol.querySelector('.parameters');

if (node) {
// if window resize happened and observer was triggered, reset previously wrapped
// parameters as they might not need wrapping anymore, and check again
node.classList.remove('wrapped');
node.querySelectorAll('.parameter .nbsp-indent').forEach((indent) => indent.remove());

const innerTextWidth = Array.from(symbol.children)
.filter((it) => !it.classList.contains('block')) // blocks are usually on their own (like annotations), so ignore it
.map((it) => it.getBoundingClientRect().width)
.reduce((a, b) => a + b, 0);

// if signature text takes up more than a single line, wrap params for readability
if (innerTextWidth > symbolBlockWidth - CODE_BLOCK_PADDING - sourceButtonWidth) {
node.classList.add('wrapped');
node.querySelectorAll('.parameter').forEach((param) => {
// has to be a physical indent so that it can be copied. styles like
// paddings and `::before { content: " " }` do not work for that
param.prepend(createNbspIndent());
});
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*!
* Copyright 2014-2025 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
@import '../_tokens/index';

/**
http://localhost:8001/jvm/org.jetbrains.dokka.uitest.markdown/-markdown-code/index.html
*/
.symbol:not(.token, .wrapped), code.block {
display: block;
box-sizing: border-box;
position: relative;
padding: 12px 16px;
border-radius: var(--size-s1);
background-color: var(--color-background-code-block);
font: var(--font-code);
white-space: pre-wrap;
overflow: scroll;
}

code.block {
overflow-x: auto;
max-width: 100%;
}

.source-link-wrapper::after {
display: block;
content: '';
clear: both;
height: 0;
}

@media screen and (width <= 759px) {
.source-link-wrapper {
margin-top: 8px;
display: block;
}
}

.source-link {
float: right;
}

/**
http://localhost:8001/jvm/org.jetbrains.dokka.uitest.types/-simple-deprecated-kotlin-class/index.html
*/

.sample-container, div.CodeMirror {
position: relative;
display: flex;
flex-direction: column;
}

.sample-container span.copy-icon {
display: none;

&::before {
width: 24px;
height: 24px;
display: inline-block;
content: '';
/* masks are required if you want to change color of the icon dynamically instead of using those provided with the SVG */
mask: url("../_assets/copy-icon.svg") no-repeat 50% 50%;
-webkit-mask-size: cover;
mask-size: cover;
background-color: var(--copy-icon-color);
}

&:hover::before {
background-color: var(--copy-icon-hover-color);
}
}

.js .sample-container:hover span.copy-icon {
display: inline-block;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
--color-background: var(--color-background-page-dt);
--text-color: var(--color-text-dt);
--text-outline: var(--color-w16);
--color-background-inline-code: var(--color-w10);
--color-background-code-block: var(--color-w05);
}

body {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ import * as tocTree from './toc-tree/index';
import * as link from './link/index';
import * as breadcrumbs from './breadcrumbs/index';
import * as inlineCode from './inline-code/index';
import * as codeBlock from './code-block/index';
import { removeBackwardCompatibilityStyles } from './utils';
import './helpers.scss';
import './global.scss';

import '@fontsource/inter/latin-400.css';
import '@fontsource/inter/latin-600.css';
import '@fontsource/jetbrains-mono/latin-400.css';
import '@fontsource/jetbrains-mono/latin-600.css';

export {
button,
checkbox,
Expand All @@ -42,6 +48,7 @@ export {
link,
breadcrumbs,
inlineCode,
codeBlock,
};

document.addEventListener('DOMContentLoaded', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@

code:not(.block) {
font: var(--font-code);
background: var(--color-background-code);
background: var(--color-background-inline-code);
display: inline-block;
padding: 0 4px;
border-radius: 2px;
}

.theme-dark code:not(.block) {
color: var(--color-text);
}
Loading

0 comments on commit 1664937

Please sign in to comment.