MarkMark parser/renderer collection - add support for new v1.1 date spec

This commit is contained in:
Garrett Mills 2024-12-31 12:28:10 -05:00
parent 5535c419be
commit 832190f875
5 changed files with 37 additions and 3 deletions

View File

@ -33,7 +33,7 @@ Tech projects that I found interesting, funny, or wanted to experiment with late
# Blogs & Posts
- Dan Luu #dev
- Dan Luu (2023-11-20) #dev
- https://danluu.com/
- https://danluu.com/everything-is-broken/
- The Case of a Curious SQL Query - Justing Jaffray #dev

View File

@ -27,4 +27,5 @@ block append style
*:not(li) > ul > li { margin-top: 20px; }
.markmark.link-tags { margin-left: 30px; }
.markmark.link-tag { color: var(--c-font-muted); }
.markmark.link-tag { color: var(--c-font-muted); font-size: 0.8em; }
.markmark.link-date { margin-left: 30px; color: var(--c-font-muted); font-size: 0.8em; }

View File

@ -20,6 +20,11 @@ export class HtmlRenderer {
for ( const link of section.links ) {
let linkTitle = `${link.title}`
if ( link.date ) {
linkTitle += ` <span class="markmark link-date">(${this.formatDate(link.date)})</span>`
}
if ( link.tags.length ) {
linkTitle += ` <span class="markmark link-tags">${link.tags.map(x => '<span class="markmark link-tag">#' + x + '</span>').join(' ')}</span>`
}
@ -39,4 +44,11 @@ export class HtmlRenderer {
return mmLines.join('\n')
}
private formatDate(date: Date): string {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are zero-based
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
}

View File

@ -61,8 +61,10 @@ export class Parser {
// If we're parsing a section list and we're NOT parsing a link's URL list
// and we encounter some text, assume it's the name of a link and start parsing it
if ( sectionListItemsRemaining && !linkListItemsRemaining && token.type === 'text' && (token as any).mmIsSectionLevel ) {
const [title, date] = this.parseTitleAndDate(token.text.split(' #')[0].trim())
currentLink = {
title: token.text.split(' #')[0].trim(),
title,
date,
tags: this.parseTags(token.text),
urls: [],
}
@ -129,6 +131,24 @@ export class Parser {
return fm
}
protected parseTitleAndDate(text: string): [string, Date|undefined] {
text = text.trim()
const dateMatcher = /(.*)\(([0-9\-+:TZ]+)\)$/g
const result = dateMatcher.exec(text)
if ( !result ) {
return [text, undefined]
}
const [, title, dateString] = result
const date = new Date(dateString)
if ( isNaN(date.getTime()) ) {
return [text, undefined]
}
return [title.trim(), date]
}
protected parseTags(text: string): string[] {
const matcher = /#([a-zA-Z0-9_\-]+)/g
return [...text.matchAll(matcher)].map(x => x[1])

View File

@ -21,6 +21,7 @@ export const isNamedSection = (what: Section): what is NamedSection =>
export type Link = {
title: string,
date?: Date,
tags: string[],
urls: string[],
}