Skip to content

Commit

Permalink
Update Server#convertMarkdown to return processed type
Browse files Browse the repository at this point in the history
  • Loading branch information
yhatt committed Jan 19, 2025
1 parent 9a8e20b commit 77210c1
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ export class Server extends (EventEmitter as new () => TypedEmitter<Server.Event
filename: string,
query: querystring.ParsedUrlQuery = {}
) {
this.converter.options.output = false
this.converter.options.pages = false
this.converter.options.type = ((): ConvertType => {
const type = ((): ConvertType => {
const queryKeys = Object.keys(query)

if (queryKeys.includes('pdf')) return ConvertType.pdf
Expand All @@ -104,10 +102,14 @@ export class Server extends (EventEmitter as new () => TypedEmitter<Server.Event
return ConvertType.html
})()

const ret = await this.converter.convertFile(new File(filename))
this.emit('converted', ret)
this.converter.options.output = false
this.converter.options.pages = false
this.converter.options.type = type

const result = await this.converter.convertFile(new File(filename))
this.emit('converted', result)

return ret
return { result, type }
}

private async loadScript() {
Expand All @@ -127,20 +129,18 @@ export class Server extends (EventEmitter as new () => TypedEmitter<Server.Event
if (!pathname) return

const qs = querystring.parse(query || '')
const response = async (fn) => {
const response = async (fn: string) => {
try {
const ret = await this.convertMarkdown(fn, qs)
const { result, type } = await this.convertMarkdown(fn, qs)

if (!ret.newFile)
if (!result.newFile)
throw new Error('Converter must return a converted file to serve.')

const { type } = this.converter.options

// Download pptx document as an attachment
if (type === ConvertType.pptx)
res.attachment(`${path.basename(fn, path.extname(fn))}.pptx`)

res.type(mimeTypes[type]).end(ret.newFile.buffer)
res.type(mimeTypes[type]).end(result.newFile.buffer)
} catch (e: unknown) {
let errorString = 'Internal server error'

Expand Down

0 comments on commit 77210c1

Please sign in to comment.