MarkMark parser/renderer collection - add support for new v1.1 date spec
This commit is contained in:
parent
5535c419be
commit
832190f875
@ -33,7 +33,7 @@ Tech projects that I found interesting, funny, or wanted to experiment with late
|
|||||||
|
|
||||||
# Blogs & Posts
|
# Blogs & Posts
|
||||||
|
|
||||||
- Dan Luu #dev
|
- Dan Luu (2023-11-20) #dev
|
||||||
- https://danluu.com/
|
- https://danluu.com/
|
||||||
- https://danluu.com/everything-is-broken/
|
- https://danluu.com/everything-is-broken/
|
||||||
- The Case of a Curious SQL Query - Justing Jaffray #dev
|
- The Case of a Curious SQL Query - Justing Jaffray #dev
|
||||||
|
@ -27,4 +27,5 @@ block append style
|
|||||||
*:not(li) > ul > li { margin-top: 20px; }
|
*:not(li) > ul > li { margin-top: 20px; }
|
||||||
|
|
||||||
.markmark.link-tags { margin-left: 30px; }
|
.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; }
|
||||||
|
@ -20,6 +20,11 @@ export class HtmlRenderer {
|
|||||||
|
|
||||||
for ( const link of section.links ) {
|
for ( const link of section.links ) {
|
||||||
let linkTitle = `${link.title}`
|
let linkTitle = `${link.title}`
|
||||||
|
|
||||||
|
if ( link.date ) {
|
||||||
|
linkTitle += ` <span class="markmark link-date">(${this.formatDate(link.date)})</span>`
|
||||||
|
}
|
||||||
|
|
||||||
if ( link.tags.length ) {
|
if ( link.tags.length ) {
|
||||||
linkTitle += ` <span class="markmark link-tags">${link.tags.map(x => '<span class="markmark link-tag">#' + x + '</span>').join(' ')}</span>`
|
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')
|
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}`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,8 +61,10 @@ export class Parser {
|
|||||||
// If we're parsing a section list and we're NOT parsing a link's URL list
|
// 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
|
// 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 ) {
|
if ( sectionListItemsRemaining && !linkListItemsRemaining && token.type === 'text' && (token as any).mmIsSectionLevel ) {
|
||||||
|
const [title, date] = this.parseTitleAndDate(token.text.split(' #')[0].trim())
|
||||||
currentLink = {
|
currentLink = {
|
||||||
title: token.text.split(' #')[0].trim(),
|
title,
|
||||||
|
date,
|
||||||
tags: this.parseTags(token.text),
|
tags: this.parseTags(token.text),
|
||||||
urls: [],
|
urls: [],
|
||||||
}
|
}
|
||||||
@ -129,6 +131,24 @@ export class Parser {
|
|||||||
return fm
|
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[] {
|
protected parseTags(text: string): string[] {
|
||||||
const matcher = /#([a-zA-Z0-9_\-]+)/g
|
const matcher = /#([a-zA-Z0-9_\-]+)/g
|
||||||
return [...text.matchAll(matcher)].map(x => x[1])
|
return [...text.matchAll(matcher)].map(x => x[1])
|
||||||
|
@ -21,6 +21,7 @@ export const isNamedSection = (what: Section): what is NamedSection =>
|
|||||||
|
|
||||||
export type Link = {
|
export type Link = {
|
||||||
title: string,
|
title: string,
|
||||||
|
date?: Date,
|
||||||
tags: string[],
|
tags: string[],
|
||||||
urls: string[],
|
urls: string[],
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user