11import * as showdown from 'showdown'
22import * as handlebars from 'handlebars'
3- import { Card } from '../trello/api'
3+ import { Attachment , Card } from '../trello/api'
44import { promises as fs } from 'fs'
55import * as path from 'path'
6- import { exec } from 'child_process'
6+ import { execSync } from 'child_process'
7+
8+ const converter = new showdown . Converter ( )
9+ const linkAttachment = ( attachment : Attachment ) =>
10+ `[${ attachment . name } ](${ attachment . url } )`
11+ const isImage = ( attachment : Attachment ) => / ^ i m a g e \/ / . test ( attachment . mimeType )
12+ const imageAttachment = (
13+ template : string ,
14+ card : Card ,
15+ attachment : Attachment ,
16+ ) => {
17+ const srcset = attachment . previews . reduce (
18+ ( srcset , { width, url } ) => ( {
19+ ...srcset ,
20+ [ `${ width } w` ] : url ,
21+ } ) ,
22+ { } as { [ key : string ] : string } ,
23+ )
24+ return handlebars . compile ( template ) ( {
25+ ...attachment ,
26+ srcset : Object . entries ( srcset )
27+ . map ( ( [ w , s ] ) => `${ s } ${ w } ` )
28+ . join ( ',\n' ) ,
29+ alt : card . name ,
30+ content : converter . makeHtml ( card . desc ) ,
31+ } )
32+ }
733
834/**
935 * Render the website
@@ -17,23 +43,42 @@ export const render = async ({
1743 webDir : string
1844 srcDir : string
1945} ) => {
20- const contentFromCards = cards . reduce (
21- ( content , { desc } ) => `${ content } \n\n${ desc } ` ,
22- '' ,
23- )
24- const contentAsMarkdown = new showdown . Converter ( ) . makeHtml ( contentFromCards )
2546 try {
2647 await fs . stat ( webDir )
2748 } catch {
2849 await fs . mkdir ( webDir )
2950 }
3051
31- const tpl = await fs . readFile ( path . join ( srcDir , 'index.html' ) , 'utf-8' )
52+ const [ tpl , imageAttachmentTemplate ] = await Promise . all ( [
53+ fs . readFile ( path . join ( srcDir , 'index.html' ) , 'utf-8' ) ,
54+ fs . readFile ( path . join ( srcDir , 'image.html' ) , 'utf-8' ) ,
55+ ] )
3256 const targetFile = path . join ( webDir , 'index.html' )
3357
58+ const contentFromCards = cards . reduce ( ( content , card ) => {
59+ const { desc, attachments } = card
60+ content = `${ content } \n\n`
61+ if ( attachments && attachments . length ) {
62+ attachments . forEach ( attachment => {
63+ if ( isImage ( attachment ) ) {
64+ content = `${ content } \n${ imageAttachment (
65+ imageAttachmentTemplate ,
66+ card ,
67+ attachment ,
68+ ) } `
69+ } else {
70+ content = `${ content } \n${ linkAttachment ( attachment ) } `
71+ }
72+ } )
73+ } else {
74+ content = `${ content } \n<section>${ converter . makeHtml ( desc ) } </section>`
75+ }
76+ return content
77+ } , '' )
78+
3479 const content = {
35- content : contentAsMarkdown ,
36- gitRev : await exec ( 'git rev-parse HEAD' )
80+ content : contentFromCards ,
81+ gitRev : execSync ( 'git rev-parse HEAD' )
3782 . toString ( )
3883 . trim ( ) ,
3984 timestamp : new Date ( ) . toISOString ( ) ,
0 commit comments