import type { BlockPlugin, HmmBlock, HmmOptions } from "../types.ts"; import { renderNode } from "../render.ts"; const REGEXP = /^(#{1,6})\s+/; const idAttribute = (text: string) => text // Remove diacritical marks .normalize("NFD") .replace(/\p{Diacritic}/gu, "") .toLowerCase() // Strip tags .replace(/<\/?[^>]+>/g, "") // Replace ampersand .replace(/&/g, "-and-") // Replace spaces with hyphen .replace(/\s+/g, "-") // Remove non-alphanumeric except hyphen .replace(/[^a-z0-9-]+/g, "") // Collapse and trim hypens .replace(/\-\-+/g, "-") .replace(/^-+/, "") .replace(/-+$/, ""); const plugin: BlockPlugin = { type: "heading", multiline: false, matchStart: (line: string): false | Array => { if (line[0] !== "#") return false; const match = line.match(REGEXP); return match ? [match[1]] : false; }, render: (block: HmmBlock, options: HmmOptions) => { let line = block.lines[0]; line = line.replace(/^#{1,6}\s+/, ""); const level = block.matches[0].length; block.render = ``; block.render += renderNode(line, options, `h${level}`); block.render += ``; return block.render; }, }; export default plugin;