|
| 1 | +import type { PhrasingContent, Root } from 'mdast' |
| 2 | +import { toString as mdastToString } from 'mdast-util-to-string' |
| 3 | +import type { Plugin } from 'unified' |
| 4 | +import { visit } from 'unist-util-visit' |
| 5 | +import { h as _h, type Properties } from 'hastscript' |
| 6 | +import type { Paragraph as P } from 'mdast' |
| 7 | + |
| 8 | +/** From Astro Starlight: Function that generates an mdast HTML tree ready for conversion to HTML by rehype. */ |
| 9 | +function h(el: string, attrs: Properties = {}, children: any[] = []): P { |
| 10 | + const { properties, tagName } = _h(el, attrs) |
| 11 | + return { |
| 12 | + children, |
| 13 | + data: { hName: tagName, hProperties: properties }, |
| 14 | + type: 'paragraph', |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +const remarkCharacterDialogue: Plugin<[{ characters: Record<string, string> }], Root> = |
| 19 | + (opts) => (tree) => { |
| 20 | + // Type guard to check if a string is a valid character dialogue key |
| 21 | + function isCharacterDialogue(s: string): s is keyof typeof opts.characters { |
| 22 | + return opts.characters.hasOwnProperty(s) && opts.characters[s] !== undefined |
| 23 | + } |
| 24 | + |
| 25 | + // Do nothing if no characters are defined |
| 26 | + if (!opts.characters || Object.keys(opts.characters).length === 0) { |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + visit(tree, (node, index, parent) => { |
| 31 | + if (!parent || index === undefined || node.type !== 'containerDirective') return |
| 32 | + |
| 33 | + const characterName = node.name |
| 34 | + if (!isCharacterDialogue(characterName)) return |
| 35 | + |
| 36 | + let title: string = characterName |
| 37 | + let titleNode: PhrasingContent[] = [{ type: 'text', value: title }] |
| 38 | + |
| 39 | + // Check if there's a custom title |
| 40 | + const firstChild = node.children[0] |
| 41 | + if ( |
| 42 | + firstChild?.type === 'paragraph' && |
| 43 | + firstChild.data && |
| 44 | + 'directiveLabel' in firstChild.data && |
| 45 | + firstChild.children.length > 0 |
| 46 | + ) { |
| 47 | + titleNode = firstChild.children |
| 48 | + title = mdastToString(firstChild.children) |
| 49 | + // The first paragraph contains a custom title, we can safely remove it. |
| 50 | + node.children.splice(0, 1) |
| 51 | + } |
| 52 | + |
| 53 | + // Do not change prefix to AD, ADM, or similar, adblocks will block the content inside. |
| 54 | + const admonition = h( |
| 55 | + 'aside', |
| 56 | + { |
| 57 | + 'aria-label': `Character dialogue: ${title}`, |
| 58 | + class: 'character-dialogue', |
| 59 | + 'data-character': characterName, |
| 60 | + }, |
| 61 | + [ |
| 62 | + // h('p', { class: 'admonition-title', 'aria-hidden': 'true' }, [...titleNode]), |
| 63 | + h('img', { |
| 64 | + class: 'character-dialogue-image', |
| 65 | + alt: title, |
| 66 | + loading: 'lazy', |
| 67 | + src: opts.characters[characterName], |
| 68 | + width: 100, |
| 69 | + }), |
| 70 | + h('div', { class: 'character-dialogue-content' }, node.children), |
| 71 | + ], |
| 72 | + ) |
| 73 | + |
| 74 | + parent.children[index] = admonition |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | +export default remarkCharacterDialogue |
0 commit comments