const std = @import("std"); const Allocator = std.mem.Allocator; const Token = @import("lexer/Token.zig"); const Lexer = @import("lexer/Lexer.zig"); const Element = @import("eval/Element.zig"); const paragraph = @import("paragraph.zig"); const testing = @import("testing.zig"); const doTest = testing.do; const doTestError = testing.doError; pub const Error = error{InvalidTitleContent} || paragraph.Error; pub fn parse(alloc: Allocator, l: *Lexer) Error!Element { const v = l.next().?; const el = try Element.Title.init(alloc, @intCast(v.content.len), paragraph.parseLine(alloc, l) catch |err| switch (err) { paragraph.Error.IllegalPlacement => return Error.InvalidTitleContent, else => return err, }); errdefer el.deinit(alloc); var next = l.next() orelse return el.element(); if (!next.kind.isDelimiter()) return Error.InvalidTitleContent; return el.element(); } test "parse title" { const alloc = std.testing.allocator; try doTest(parse, alloc, "# hey", "

hey

"); try doTest(parse, alloc, "## hey", "

hey

"); try doTest(parse, alloc, "### hey", "

hey

"); try doTest(parse, alloc, "# hello *world*", "

hello world

"); try doTestError(parse, alloc, "# aa :::", Error.InvalidTitleContent); }