@@ -6,24 +6,22 @@ import markdownpdf from "markdown-pdf";
66import shortId from "shortid" ;
77import querystring from "querystring" ;
88import moment from "moment" ;
9- // const { Pandoc } = require(' @hackmd/pandoc.js')
9+ import { InputFormat , OutputFormat , Pandoc } from " @hackmd/pandoc.js" ;
1010
1111import config from "../config" ;
1212import { logger } from "../logger" ;
13- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
14- // @ts -ignore
1513import { Note , Revision } from "../models" ;
1614import { errorInternalError , errorNotFound } from "../response" ;
1715
18- export function actionPublish ( req : Request , res : Response , note ) : void {
16+ export function actionPublish ( req : Request , res : Response , note : Note ) : void {
1917 res . redirect ( config . serverURL + '/s/' + ( note . alias || note . shortid ) )
2018}
2119
22- export function actionSlide ( req : Request , res : Response , note ) : void {
20+ export function actionSlide ( req : Request , res : Response , note : Note ) : void {
2321 res . redirect ( config . serverURL + '/p/' + ( note . alias || note . shortid ) )
2422}
2523
26- export function actionDownload ( req : Request , res : Response , note ) : void {
24+ export function actionDownload ( req : Request , res : Response , note : Note ) : void {
2725 const body = note . content
2826 const title = Note . decodeTitle ( note . title )
2927 const filename = encodeURIComponent ( title )
@@ -39,7 +37,7 @@ export function actionDownload(req: Request, res: Response, note): void {
3937 res . send ( body )
4038}
4139
42- export function actionInfo ( req : Request , res : Response , note ) : void {
40+ export function actionInfo ( req : Request , res : Response , note : Note ) : void {
4341 const body = note . content
4442 const extracted = Note . extractMeta ( body )
4543 const markdown = extracted . markdown
@@ -66,7 +64,7 @@ export function actionInfo(req: Request, res: Response, note): void {
6664 res . send ( data )
6765}
6866
69- export function actionPDF ( req : Request , res : Response , note ) : void {
67+ export function actionPDF ( req : Request , res : Response , note : Note ) : void {
7068 const url = config . serverURL || 'http://' + req . get ( 'host' )
7169 const body = note . content
7270 const extracted = Note . extractMeta ( body )
@@ -119,50 +117,50 @@ const outputFormats = {
119117 docx : 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
120118}
121119
122- export async function actionPandoc ( req : Request , res : Response , note ) : Promise < void > {
123- // var url = config.serverURL || 'http://' + req.get('host')
124- // var body = note.content
125- // var extracted = Note.extractMeta(body)
126- // var content = extracted.markdown
127- // var title = Note.decodeTitle(note.title)
128- //
129- // if (!fs.existsSync(config.tmpPath)) {
130- // fs.mkdirSync(config.tmpPath)
131- // }
132- // const pandoc = new Pandoc()
133- //
134- // var path = config.tmpPath + '/' + Date.now()
135- // content = content.replace(/\]\(\//g, '](' + url + '/')
136- //
137- // // TODO: check export type
138- // const { exportType } = req.query
139- //
140- // try {
141- // // TODO: timeout rejection
142- //
143- // await pandoc.convertToFile(content, ' markdown' , exportType, path, [
144- // '--metadata', `title=${title}`
145- // ])
146- //
147- // var stream = fs.createReadStream(path)
148- // var filename = title
149- // // Be careful of special characters
150- // filename = encodeURIComponent(filename)
151- // // Ideally this should strip them
152- // res.setHeader('Content-disposition', `attachment; filename="${filename}.${exportType}"`)
153- // res.setHeader('Cache-Control', 'private')
154- // res.setHeader('Content-Type', `${outputFormats[exportType]}; charset=UTF-8`)
155- // res.setHeader('X-Robots-Tag', 'noindex, nofollow') // prevent crawling
156- // stream.pipe(res)
157- // } catch (err) {
158- // // TODO: handle error
159- // res.json({
160- // message: err.message
161- // })
162- // }
120+ export async function actionPandoc ( req : Request , res : Response , note : Note ) : Promise < void > {
121+ const url = config . serverURL || 'http://' + req . get ( 'host' )
122+ const body = note . content
123+ const extracted = Note . extractMeta ( body )
124+ let content = extracted . markdown
125+ const title = Note . decodeTitle ( note . title )
126+
127+ if ( ! fs . existsSync ( config . tmpPath ) ) {
128+ fs . mkdirSync ( config . tmpPath )
129+ }
130+ const pandoc = new Pandoc ( )
131+
132+ const path = config . tmpPath + '/' + Date . now ( )
133+ content = content . replace ( / \] \( \/ / g, '](' + url + '/' )
134+
135+ // TODO: check export type
136+ const exportType = req . query . exportType as string
137+
138+ try {
139+ // TODO: timeout rejection
140+
141+ await pandoc . convertToFile ( content , InputFormat . markdown , exportType as OutputFormat , path , [
142+ '--metadata' , `title=${ title } `
143+ ] )
144+
145+ const stream = fs . createReadStream ( path )
146+ let filename = title
147+ // Be careful of special characters
148+ filename = encodeURIComponent ( filename )
149+ // Ideally this should strip them
150+ res . setHeader ( 'Content-disposition' , `attachment; filename="${ filename } .${ exportType } "` )
151+ res . setHeader ( 'Cache-Control' , 'private' )
152+ res . setHeader ( 'Content-Type' , `${ outputFormats [ exportType ] } ; charset=UTF-8` )
153+ res . setHeader ( 'X-Robots-Tag' , 'noindex, nofollow' ) // prevent crawling
154+ stream . pipe ( res )
155+ } catch ( err ) {
156+ // TODO: handle error
157+ res . json ( {
158+ message : err . message
159+ } )
160+ }
163161}
164162
165- export function actionGist ( req : Request , res : Response , note ) : void {
163+ export function actionGist ( req : Request , res : Response , note : Note ) : void {
166164 const data = {
167165 client_id : config . github . clientID ,
168166 redirect_uri : config . serverURL + '/auth/github/callback/' + Note . encodeNoteId ( note . id ) + '/gist' ,
@@ -173,7 +171,7 @@ export function actionGist(req: Request, res: Response, note): void {
173171 res . redirect ( 'https://github.com/login/oauth/authorize?' + query )
174172}
175173
176- export function actionRevision ( req : Request , res : Response , note ) : void {
174+ export function actionRevision ( req : Request , res : Response , note : Note ) : void {
177175 const actionId = req . params . actionId
178176 if ( actionId ) {
179177 const time = moment ( parseInt ( actionId ) )
0 commit comments