Skip to content

Commit 17987fe

Browse files
committed
ts: lib/models/note.js
1 parent d11dcc2 commit 17987fe

File tree

1 file changed

+62
-35
lines changed

1 file changed

+62
-35
lines changed

lib/models/note.js renamed to lib/models/note.ts

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
1-
'use strict'
21
// external modules
3-
var fs = require('fs')
4-
var path = require('path')
5-
var LZString = require('@hackmd/lz-string')
6-
var base64url = require('base64url')
7-
var md = require('markdown-it')()
8-
var metaMarked = require('@hackmd/meta-marked')
9-
var cheerio = require('cheerio')
10-
var shortId = require('shortid')
11-
var Sequelize = require('sequelize')
12-
var async = require('async')
13-
var moment = require('moment')
14-
var DiffMatchPatch = require('@hackmd/diff-match-patch')
15-
var dmp = new DiffMatchPatch()
16-
17-
const { stripTags } = require('../string')
2+
import * as fs from "fs";
3+
import * as path from "path";
4+
import * as LZString from "@hackmd/lz-string";
5+
import base64url from "base64url";
6+
import * as markdown_it from "markdown-it";
7+
import * as metaMarked from "@hackmd/meta-marked";
8+
import * as cheerio from "cheerio";
9+
import * as shortId from "shortid";
10+
import * as Sequelize from "sequelize";
11+
import * as async from "async";
12+
import * as moment from "moment";
13+
import * as DiffMatchPatch from "@hackmd/diff-match-patch";
1814

1915
// core
20-
var config = require('../config')
21-
var logger = require('../logger')
16+
import * as config from "../config";
17+
import * as logger from "../logger";
18+
import {stripTags} from "../string";
2219

2320
// ot
24-
var ot = require('../ot')
21+
import * as ot from "../ot";
2522

23+
var md = markdown_it()
24+
var dmp = new DiffMatchPatch()
2625
// permission types
2726
var permissionTypes = ['freely', 'editable', 'limited', 'locked', 'protected', 'private']
2827

29-
module.exports = function (sequelize, DataTypes) {
28+
export = function (sequelize, DataTypes) {
3029
var Note = sequelize.define('Note', {
3130
id: {
3231
type: DataTypes.UUID,
@@ -186,7 +185,11 @@ module.exports = function (sequelize, DataTypes) {
186185
Note.checkNoteIdValid = function (id) {
187186
var uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
188187
var result = id.match(uuidRegex)
189-
if (result && result.length === 1) { return true } else { return false }
188+
if (result && result.length === 1) {
189+
return true
190+
} else {
191+
return false
192+
}
190193
}
191194
Note.parseNoteIdAsync = function (noteId) {
192195
return new Promise((resolve, reject) => {
@@ -199,7 +202,7 @@ module.exports = function (sequelize, DataTypes) {
199202
})
200203
}
201204

202-
async function syncNote (noteInFS, note) {
205+
async function syncNote(noteInFS, note) {
203206
const contentLength = noteInFS.content.length
204207

205208
let note2 = await note.update({
@@ -276,7 +279,11 @@ module.exports = function (sequelize, DataTypes) {
276279
// try to parse note id by LZString Base64
277280
try {
278281
var id = LZString.decompressFromBase64(noteId)
279-
if (id && Note.checkNoteIdValid(id)) { return callback(null, id) } else { return _callback(null, null) }
282+
if (id && Note.checkNoteIdValid(id)) {
283+
return callback(null, id)
284+
} else {
285+
return _callback(null, null)
286+
}
280287
} catch (err) {
281288
if (err.message === 'Cannot read property \'charAt\' of undefined') {
282289
logger.warning('Looks like we can not decode "' + noteId + '" with LZString. Can be ignored.')
@@ -290,7 +297,11 @@ module.exports = function (sequelize, DataTypes) {
290297
// try to parse note id by base64url
291298
try {
292299
var id = Note.decodeNoteId(noteId)
293-
if (id && Note.checkNoteIdValid(id)) { return callback(null, id) } else { return _callback(null, null) }
300+
if (id && Note.checkNoteIdValid(id)) {
301+
return callback(null, id)
302+
} else {
303+
return _callback(null, null)
304+
}
294305
} catch (err) {
295306
logger.error(err)
296307
return _callback(null, null)
@@ -344,7 +355,9 @@ module.exports = function (sequelize, DataTypes) {
344355
title = meta.title
345356
} else {
346357
var h1s = $('h1')
347-
if (h1s.length > 0 && h1s.first().text().split('\n').length === 1) { title = stripTags(h1s.first().text()) }
358+
if (h1s.length > 0 && h1s.first().text().split('\n').length === 1) {
359+
title = stripTags(h1s.first().text())
360+
}
348361
}
349362
if (!title) title = 'Untitled'
350363
return title
@@ -393,7 +406,9 @@ module.exports = function (sequelize, DataTypes) {
393406
break
394407
}
395408
}
396-
if (!found) { tags.push(rawtags[i]) }
409+
if (!found) {
410+
tags.push(rawtags[i])
411+
}
397412
}
398413
return tags
399414
}
@@ -412,14 +427,26 @@ module.exports = function (sequelize, DataTypes) {
412427
return obj
413428
}
414429
Note.parseMeta = function (meta) {
415-
var _meta = {}
430+
var _meta: any = {}
416431
if (meta) {
417-
if (meta.title && (typeof meta.title === 'string' || typeof meta.title === 'number')) { _meta.title = meta.title }
418-
if (meta.description && (typeof meta.description === 'string' || typeof meta.description === 'number')) { _meta.description = meta.description }
419-
if (meta.robots && (typeof meta.robots === 'string' || typeof meta.robots === 'number')) { _meta.robots = meta.robots }
420-
if (meta.GA && (typeof meta.GA === 'string' || typeof meta.GA === 'number')) { _meta.GA = meta.GA }
421-
if (meta.disqus && (typeof meta.disqus === 'string' || typeof meta.disqus === 'number')) { _meta.disqus = meta.disqus }
422-
if (meta.slideOptions && (typeof meta.slideOptions === 'object')) { _meta.slideOptions = meta.slideOptions }
432+
if (meta.title && (typeof meta.title === 'string' || typeof meta.title === 'number')) {
433+
_meta.title = meta.title
434+
}
435+
if (meta.description && (typeof meta.description === 'string' || typeof meta.description === 'number')) {
436+
_meta.description = meta.description
437+
}
438+
if (meta.robots && (typeof meta.robots === 'string' || typeof meta.robots === 'number')) {
439+
_meta.robots = meta.robots
440+
}
441+
if (meta.GA && (typeof meta.GA === 'string' || typeof meta.GA === 'number')) {
442+
_meta.GA = meta.GA
443+
}
444+
if (meta.disqus && (typeof meta.disqus === 'string' || typeof meta.disqus === 'number')) {
445+
_meta.disqus = meta.disqus
446+
}
447+
if (meta.slideOptions && (typeof meta.slideOptions === 'object')) {
448+
_meta.slideOptions = meta.slideOptions
449+
}
423450
}
424451
return _meta
425452
}
@@ -584,7 +611,7 @@ module.exports = function (sequelize, DataTypes) {
584611
return operations
585612
}
586613

587-
function readFileSystemNote (filePath) {
614+
function readFileSystemNote(filePath) {
588615
const fsModifiedTime = moment(fs.statSync(filePath).mtime)
589616
const content = fs.readFileSync(filePath, 'utf8')
590617

@@ -595,7 +622,7 @@ module.exports = function (sequelize, DataTypes) {
595622
}
596623
}
597624

598-
function shouldSyncNote (note, noteInFS) {
625+
function shouldSyncNote(note, noteInFS) {
599626
const dbModifiedTime = moment(note.lastchangeAt || note.createdAt)
600627
return noteInFS.lastchangeAt.isAfter(dbModifiedTime) && note.content !== noteInFS.content
601628
}

0 commit comments

Comments
 (0)