Skip to content

Commit 2e67d0c

Browse files
committed
lint: lib/note/noteActions.ts
- reactive pandoc - add typing annotate
1 parent b42dbfc commit 2e67d0c

File tree

2 files changed

+58
-60
lines changed

2 files changed

+58
-60
lines changed

lib/note/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
import {Request, Response} from "express";
22
import config from "../config";
3+
import {historyDelete, updateHistory} from "../history";
34
import {logger} from "../logger";
45

56
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
67
// @ts-ignore
78
import {Note, Revision, User} from "../models";
89

10+
import * as realtime from "../realtime/realtime";
11+
912

1013
import {errorForbidden, errorInternalError, errorNotFound, newCheckViewPermission, responseCodiMD} from "../response";
11-
import {historyDelete, updateHistory} from "../history";
1214
import {createNoteWithRevision} from "../services/note";
1315
import {
1416
actionDownload,
1517
actionGist,
16-
actionInfo, actionPandoc,
18+
actionInfo,
19+
actionPandoc,
1720
actionPDF,
1821
actionPublish,
1922
actionRevision,
2023
actionSlide
2124
} from "./noteActions";
2225

23-
import * as realtime from "../realtime/realtime";
24-
25-
async function getNoteById(noteId, {includeUser} = {includeUser: false}) {
26+
async function getNoteById(noteId, {includeUser} = {includeUser: false}): Promise<Note> {
2627
const id = await Note.parseNoteIdAsync(noteId)
2728

2829
const includes = []
@@ -37,13 +38,12 @@ async function getNoteById(noteId, {includeUser} = {includeUser: false}) {
3738
})
3839
}
3940

40-
const note = await Note.findOne({
41+
return Note.findOne({
4142
where: {
4243
id: id
4344
},
4445
include: includes
45-
})
46-
return note
46+
});
4747
}
4848

4949
async function createNote(userId, noteAlias) {
@@ -92,7 +92,7 @@ export async function showNote(req: Request, res: Response): Promise<void> {
9292
return responseCodiMD(res, note)
9393
}
9494

95-
function canViewNote(note, isLogin, userId) {
95+
function canViewNote(note: Note, isLogin: boolean, userId: string | null): boolean {
9696
if (note.permission === 'private') {
9797
return note.ownerId === userId
9898
}

lib/note/noteActions.ts

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,22 @@ import markdownpdf from "markdown-pdf";
66
import shortId from "shortid";
77
import querystring from "querystring";
88
import moment from "moment";
9-
// const { Pandoc } = require('@hackmd/pandoc.js')
9+
import {InputFormat, OutputFormat, Pandoc} from "@hackmd/pandoc.js";
1010

1111
import config from "../config";
1212
import {logger} from "../logger";
13-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
14-
// @ts-ignore
1513
import {Note, Revision} from "../models";
1614
import {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

Comments
 (0)