PageSourceSearch

https://blog.kurokobo.com/js/paragraph-indent.js

js kurokobo.com collected 2026-10-05 20:12:47 UTC 1,843 bytes, 47 lines download raw bytes

1(function () {
2  // Opening punctuation that should suppress indentation:
3  // 「』【〔(〈《[{""''〝‛‟ and ASCII equivalents [ ( { ' "
4  const NO_INDENT_CHARS = /^[「『【〔(〈《[{""''〝‛‟〈《〔【\[({'"]/
5
6  document.querySelectorAll('.article-content > p').forEach(function (p) {
7    // Grab the text content of the first non-BR child node
8    var firstText = ''
9    for (var i = 0; i < p.childNodes.length; i++) {
10      var node = p.childNodes[i]
11      if (node.nodeType === Node.TEXT_NODE) {
12        firstText = node.textContent
13        break
14      } else if (node.nodeType === Node.ELEMENT_NODE && node.tagName !== 'BR') {
15        firstText = node.textContent
16        break
17      }
18    }
19    // Suppress indent when the paragraph starts with an opening bracket
20    if (NO_INDENT_CHARS.test(firstText.trimStart())) {
21      p.classList.add('no-indent')
22    }
23
24    // Insert an ideographic space (U+3000) after each <br> to simulate indent
25    p.querySelectorAll('br').forEach(function (br) {
26      var next = br.nextSibling
27      // Skip if already inserted
28      if (next && next.nodeType === Node.TEXT_NODE && next.textContent.startsWith('\u3000')) {
29        return
30      }
31      var nextText = ''
32      if (next && next.nodeType === Node.TEXT_NODE) {
33        // Strip leading whitespace caused by the source newline after <br>
34        var trimmed = next.textContent.replace(/^[ \t\r\n]+/, '')
35        nextText = trimmed
36        next.textContent = trimmed
37      } else if (next && next.nodeType === Node.ELEMENT_NODE) {
38        nextText = next.textContent
39      }
40      // Skip if the line starts with an opening bracket
41      if (NO_INDENT_CHARS.test(nextText)) {
42        return
43      }
44      br.after(document.createTextNode('\u3000'))
45    })
46  })
47})()

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.