progfay/scrapbox-parser

View on GitHub
src/block/node/InternalLinkNode.ts

Summary

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

import type { NodeCreator } from "./creator.ts";
import type { LinkNode } from "./type.ts";

const internalLinkRegExp = /\[\/?[^[\]]+\]/;

const createInternalLinkNode: NodeCreator<LinkNode> = (raw) => {
    const href = raw.substring(1, raw.length - 1);
    return [
        {
            type: "link",
            raw,
            pathType: href.startsWith("/") ? "root" : "relative",
            href,
            content: "",
        },
    ];
};

export const InternalLinkNodeParser = createNodeParser(createInternalLinkNode, {
    parseOnNested: true,
    parseOnQuoted: true,
    patterns: [internalLinkRegExp],
});