(
root: ParentNode,
options: CodeBlockEnhanceOptions = {}
)
| 16 | } |
| 17 | |
| 18 | export function enhanceCodeBlockCopy( |
| 19 | root: ParentNode, |
| 20 | options: CodeBlockEnhanceOptions = {} |
| 21 | ): void { |
| 22 | const blocks = Array.from(root.querySelectorAll<HTMLPreElement>('pre')) |
| 23 | const storageKey = options.notePath ? codeBlockFoldStorageKey(options.notePath) : null |
| 24 | let blockIndex = 0 |
| 25 | |
| 26 | for (const pre of blocks) { |
| 27 | const code = pre.firstElementChild |
| 28 | if (!(code instanceof HTMLElement) || code.tagName.toLowerCase() !== 'code') { |
| 29 | continue |
| 30 | } |
| 31 | |
| 32 | const index = blockIndex |
| 33 | blockIndex += 1 |
| 34 | const wrapper = ensureCodeBlockWrapper(pre) |
| 35 | wrapper.setAttribute(CODE_BLOCK_INDEX_ATTR, String(index)) |
| 36 | if (storageKey) wrapper.setAttribute(CODE_BLOCK_STORAGE_KEY_ATTR, storageKey) |
| 37 | else wrapper.removeAttribute(CODE_BLOCK_STORAGE_KEY_ATTR) |
| 38 | |
| 39 | ensureCodeBlockToolbar(wrapper, pre) |
| 40 | ensureCodeBlockSummary(wrapper, code) |
| 41 | |
| 42 | const persisted = storageKey ? readPersistedFoldState(storageKey, index) : null |
| 43 | applyCodeBlockFoldState(wrapper, persisted ?? false) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | export function getCodeBlockTextForCopyButton(button: Element): string | null { |
| 48 | const block = button.closest(`.${CODE_BLOCK_CLASS}`) |
no test coverage detected