CoolFace
Datasetpublic

gradio/frontend

sourceHugging Faceupdated 14h agoView on Hugging Face
1likes403kdownloads
frontmatter.ts62 linesDownload Raw Back to shared
1import type {2	Element,3	MarkdownExtension,4	BlockContext,5	Line6} from "@lezer/markdown";7import { parseMixed } from "@lezer/common";8import { yaml } from "@codemirror/legacy-modes/mode/yaml";9import { foldInside, foldNodeProp, StreamLanguage } from "@codemirror/language";10import { styleTags, tags } from "@lezer/highlight";11 12const frontMatterFence = /^---\s*$/m;13 14export const frontmatter: MarkdownExtension = {15	defineNodes: [{ name: "Frontmatter", block: true }, "FrontmatterMark"],16	props: [17		styleTags({18			Frontmatter: [tags.documentMeta, tags.monospace],19			FrontmatterMark: tags.processingInstruction20		}),21		foldNodeProp.add({22			Frontmatter: foldInside,23			FrontmatterMark: () => null24		})25	],26	wrap: parseMixed((node) => {27		const { parser } = StreamLanguage.define(yaml);28		if (node.type.name === "Frontmatter") {29			return {30				parser,31				overlay: [{ from: node.from + 4, to: node.to - 4 }]32			};33		}34		return null;35	}),36	parseBlock: [37		{38			name: "Frontmatter",39			before: "HorizontalRule",40			parse: (cx: BlockContext, line: Line): boolean => {41				let end: number | undefined = undefined;42				const children = new Array<Element>();43				if (cx.lineStart === 0 && frontMatterFence.test(line.text)) {44					children.push(cx.elt("FrontmatterMark", 0, 4));45					while (cx.nextLine()) {46						if (frontMatterFence.test(line.text)) {47							end = cx.lineStart + 4;48							break;49						}50					}51					if (end !== undefined) {52						children.push(cx.elt("FrontmatterMark", end - 4, end));53						cx.addElement(cx.elt("Frontmatter", 0, end, children));54					}55					return true;56				}57				return false;58			}59		}60	]61};62 
gradio/frontend · CoolFace