@@ -6,24 +6,22 @@ import markdownpdf from "markdown-pdf";
6
6
import shortId from "shortid" ;
7
7
import querystring from "querystring" ;
8
8
import moment from "moment" ;
9
- // const { Pandoc } = require(' @hackmd/pandoc.js')
9
+ import { InputFormat , OutputFormat , Pandoc } from " @hackmd/pandoc.js" ;
10
10
11
11
import config from "../config" ;
12
12
import { logger } from "../logger" ;
13
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
14
- // @ts -ignore
15
13
import { Note , Revision } from "../models" ;
16
14
import { errorInternalError , errorNotFound } from "../response" ;
17
15
18
- export function actionPublish ( req : Request , res : Response , note ) : void {
16
+ export function actionPublish ( req : Request , res : Response , note : Note ) : void {
19
17
res . redirect ( config . serverURL + '/s/' + ( note . alias || note . shortid ) )
20
18
}
21
19
22
- export function actionSlide ( req : Request , res : Response , note ) : void {
20
+ export function actionSlide ( req : Request , res : Response , note : Note ) : void {
23
21
res . redirect ( config . serverURL + '/p/' + ( note . alias || note . shortid ) )
24
22
}
25
23
26
- export function actionDownload ( req : Request , res : Response , note ) : void {
24
+ export function actionDownload ( req : Request , res : Response , note : Note ) : void {
27
25
const body = note . content
28
26
const title = Note . decodeTitle ( note . title )
29
27
const filename = encodeURIComponent ( title )
@@ -39,7 +37,7 @@ export function actionDownload(req: Request, res: Response, note): void {
39
37
res . send ( body )
40
38
}
41
39
42
- export function actionInfo ( req : Request , res : Response , note ) : void {
40
+ export function actionInfo ( req : Request , res : Response , note : Note ) : void {
43
41
const body = note . content
44
42
const extracted = Note . extractMeta ( body )
45
43
const markdown = extracted . markdown
@@ -66,7 +64,7 @@ export function actionInfo(req: Request, res: Response, note): void {
66
64
res . send ( data )
67
65
}
68
66
69
- export function actionPDF ( req : Request , res : Response , note ) : void {
67
+ export function actionPDF ( req : Request , res : Response , note : Note ) : void {
70
68
const url = config . serverURL || 'http://' + req . get ( 'host' )
71
69
const body = note . content
72
70
const extracted = Note . extractMeta ( body )
@@ -119,50 +117,50 @@ const outputFormats = {
119
117
docx : 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
120
118
}
121
119
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
+ }
163
161
}
164
162
165
- export function actionGist ( req : Request , res : Response , note ) : void {
163
+ export function actionGist ( req : Request , res : Response , note : Note ) : void {
166
164
const data = {
167
165
client_id : config . github . clientID ,
168
166
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 {
173
171
res . redirect ( 'https://github.com/login/oauth/authorize?' + query )
174
172
}
175
173
176
- export function actionRevision ( req : Request , res : Response , note ) : void {
174
+ export function actionRevision ( req : Request , res : Response , note : Note ) : void {
177
175
const actionId = req . params . actionId
178
176
if ( actionId ) {
179
177
const time = moment ( parseInt ( actionId ) )
0 commit comments