This repository has been archived on 2026-03-01. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
www/src/markmark/html.renderer.ts

55 lines
2.0 KiB
TypeScript

import * as marked from 'marked'
import {isNamedSection, MarkMark} from './types'
export class HtmlRenderer {
public render(mm: MarkMark): string {
let mmLines: string[] = []
for ( const section of mm.sections ) {
mmLines.push('<section class="markmark section">')
// if this section has a title/description, write those out
if ( isNamedSection(section) ) {
mmLines.push(`<h1 class="markmark section-title">${marked.marked.parse(section.title)}</h1>`)
if ( section.description ) {
mmLines.push(`<p class="markmark section-description">${marked.marked.parse(section.description)}</p>`)
}
}
mmLines.push('<ul class="markmark section-list">')
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>`
}
mmLines.push(`<li class="markmark link-title" id="link-${link.hash}">${marked.marked.parse(linkTitle)}<ul class="markmark url-list">`)
for ( const url of link.urls ) {
mmLines.push(`<li class="markmark link-url"><a href="${url}" target="_blank">${url}</a></li>`)
}
mmLines.push('</ul></li>')
}
mmLines.push('</ul>')
mmLines.push('</section>')
}
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}`;
}
}