Skip to content

Commit

Permalink
feat(hiccup-markdown): add metadata support for <hr> tags
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 26, 2023
1 parent b7c310f commit d5c84c9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
10 changes: 8 additions & 2 deletions packages/hiccup-markdown/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ export interface TagTransforms {
footnote(ctx: MDParseContext, id: string, body: any[], meta?: any): any;
footnoteRef(ctx: MDParseContext, id: string): any;
footnoteWrapper(ctx: MDParseContext, notes: IObjectOf<any>): any;
heading(ctx: MDParseContext, level: number, id: string, body: any[], meta?: any): any;
hr(ctx: MDParseContext, length: number): any;
heading(
ctx: MDParseContext,
level: number,
id: string,
body: any[],
meta?: any
): any;
hr(ctx: MDParseContext, length: number, meta?: any): any;
img(ctx: MDParseContext, label: string, src: string, title?: string): any;
italic(ctx: MDParseContext, body: any[]): any;
kbd(ctx: MDParseContext, key: string): any;
Expand Down
8 changes: 5 additions & 3 deletions packages/hiccup-markdown/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export const DEFAULT_TAG_TRANSFORMS: TagTransforms = {
withMeta({ id }, meta),
...body,
],
hr: (_, __length) => ["hr", { __length }],
hr: (_, __length, meta?) => ["hr", withMeta({ __length }, meta)],
img: (_, alt, src, title) => ["img", { src, alt, title }],
italic: (_, body) => ["em", {}, ...body],
kbd: (_, key) => ["kbd", {}, key],
Expand Down Expand Up @@ -415,8 +415,10 @@ export const walk: Fn3<
ctx.meta = null;
},

hr: (scope, ctx, acc) =>
__collect(acc, ctx.tags.hr(ctx, scope.result.length)),
hr: (scope, ctx, acc) => {
__collect(acc, ctx.tags.hr(ctx, scope.result.length, ctx.meta));
ctx.meta = null;
},

img: ({ children }, ctx, acc) =>
__collect(
Expand Down
6 changes: 6 additions & 0 deletions packages/hiccup-markdown/test/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,12 @@ with linebreaks
]);
},

"meta hr": () => {
check("{{{ front matter }}}\n---", [
["hr", { __length: 3, __meta: "front matter" }],
]);
},

"meta list": () => {
check("{{{ foo }}}\n- Hello", [
["ul", { __meta: "foo" }, ["li", {}, "Hello"]],
Expand Down

0 comments on commit d5c84c9

Please sign in to comment.