progfay/scrapbox-parser

View on GitHub
src/block/CodeBlock.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import type { Row } from "./Row.ts";

export interface CodeBlockPack {
    type: "codeBlock";
    rows: Row[];
}

/**
 * Scrapbox {@link https://scrapbox.io/help/Syntax#58348ae2651ee500008d67df | code block} type
 */
export interface CodeBlock {
    indent: number;
    type: "codeBlock";
    fileName: string;
    content: string;
}

export const convertToCodeBlock = (pack: CodeBlockPack): CodeBlock => {
    const {
        rows: [head, ...body],
    } = pack;
    const { indent = 0, text = "" } = head ?? {};
    const fileName: string = text.replace(/^\s*code:/, "");

    return {
        indent,
        type: "codeBlock",
        fileName,
        content: body
            .map((row: Row): string => row.text.substring(indent + 1))
            .join("\n"),
    };
};