Type script

H20 tháng 4, 2024

TABLE OF CONTENTS

Typescript highlight

import { visit } from 'unist-util-visit'

export default function remarkHighlightBlock() {
    /**
     * Transform.
     *
     * @param {Root} tree
     *   Tree.
     * @returns {undefined}
     *   Nothing.
     */
    return function (tree: any) {
        visit(tree, function (node: any, index, parent: any) {

            const blocks = ["quote-info", "quote-success", "quote-warning", "quote-danger"]
            if (node.type === 'code' && blocks.includes(node.lang)) {
                const content = node.value; // Extract content

                // Create a new paragraph element with the desired class
                const newParagraph = {
                    type: 'blockquote',
                    // depth: 2,
                    properties: { mdastId: "block", className: `md-quote ${node.lang}`},
                    value: content,
                };

                // Replace the code block with the new paragraph
                parent.children.splice(parent.children.indexOf(node), 1, newParagraph);
            }

        })
    }
}