Code Block Test

2 minute read

String

Using indents:

text
text
text

Fenced code block:

text
text
<tag>

Fenced code block with language (lineNumbersInTable = false):

 1// JavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJava
 2public final class String
 3    implements java.io.Serializable, Comparable<String>, CharSequence
 4{
 5    /** The value is used for character storage. */
 6    private final char value[];
 7
 8    /** The offset is the first index of the storage that is used. */
 9    private final int offset;
10
11    /** The count is the number of characters in the String. */
12    private final int count;
13
14    /** Cache the hash code for the string */
15    private int hash; // Default to 0
16
17    // ...
18
19    // Package private constructor which shares value array for speed.
20    String(int offset, int count, char value[]) {
21        this.value = value;
22        this.offset = offset;
23        this.count = count;
24    }
25
26    // ...
27
28    /**
29     * Returns a new string that is a substring of this string. The
30     * substring begins at the specified <code>beginIndex</code> and
31     * extends to the character at index <code>endIndex - 1</code>.
32     * Thus the length of the substring is <code>endIndex-beginIndex</code>.
33     * <p>
34     * Examples:
35     * <blockquote><pre>
36     * "hamburger".substring(4, 8) returns "urge"
37     * "smiles".substring(1, 5) returns "mile"
38     * </pre></blockquote>
39     *
40     * @param      beginIndex   the beginning index, inclusive.
41     * @param      endIndex     the ending index, exclusive.
42     * @return     the specified substring.
43     * @exception  IndexOutOfBoundsException  if the
44     *             <code>beginIndex</code> is negative, or
45     *             <code>endIndex</code> is larger than the length of
46     *             this <code>String</code> object, or
47     *             <code>beginIndex</code> is larger than
48     *             <code>endIndex</code>.
49     */
50    public String substring(int beginIndex, int endIndex) {
51        if (beginIndex < 0) {
52            throw new StringIndexOutOfBoundsException(beginIndex);
53        }
54        if (endIndex > count) {
55            throw new StringIndexOutOfBoundsException(endIndex);
56        }
57        if (beginIndex > endIndex) {
58            throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
59        }
60        return ((beginIndex == 0) && (endIndex == count)) ? this :
61            new String(offset + beginIndex, endIndex - beginIndex, value);
62    }
63
64    // ...
65}

Using hugo’s highlight shortcode (lineNumbersInTable = true):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
// TypeScript
type OptionalUser = {
    [K in keyof User]?: User[K]
}

// ! we can remove optional constraint
type RequiredUser = {
    [K in keyof OptionalUser]-?: User[K]
}

type NullableUser = {
    [K in keyof User]: User[K] | null
}
type ReadonlyUser = {
    readonly [K in keyof User]: User[K]
}

// ! we can remove readonly constraint
type ModifiableUser = {
    -readonly [K in keyof User]: User[K]
}

Without line number

(() => {

  function createCopyButton(codeNode) {
    const copyBtn = document.createElement('button')
    copyBtn.className = 'code-copy-btn'
    copyBtn.type = 'button'
    copyBtn.innerText = 'copy'
    copyBtn.parentElement = codeNode.parentElement

    let resetTimer
    copyBtn.addEventListener('click', () => {
      navigator.clipboard.writeText(codeNode.innerText).then(() => {
        copyBtn.innerText = 'copied!'
      }).then(() => {
        clearTimeout(resetTimer)
        resetTimer = setTimeout(() => {
          copyBtn.innerText = 'copy'
        }, 1000)
      })
    })

    return copyBtn
  }

  document.querySelectorAll('pre > code').forEach((codeNode) => {
    const copyBtn = createCopyButton(codeNode);
    codeNode.parentNode.insertBefore(copyBtn, codeNode)
  })

})()