1
- 'use strict'
2
1
// 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" ;
18
14
19
15
// 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" ;
22
19
23
20
// ot
24
- var ot = require ( ' ../ot' )
21
+ import * as ot from " ../ot" ;
25
22
23
+ var md = markdown_it ( )
24
+ var dmp = new DiffMatchPatch ( )
26
25
// permission types
27
26
var permissionTypes = [ 'freely' , 'editable' , 'limited' , 'locked' , 'protected' , 'private' ]
28
27
29
- module . exports = function ( sequelize , DataTypes ) {
28
+ export = function ( sequelize , DataTypes ) {
30
29
var Note = sequelize . define ( 'Note' , {
31
30
id : {
32
31
type : DataTypes . UUID ,
@@ -186,7 +185,11 @@ module.exports = function (sequelize, DataTypes) {
186
185
Note . checkNoteIdValid = function ( id ) {
187
186
var uuidRegex = / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 1 - 5 ] [ 0 - 9 a - f ] { 3 } - [ 8 9 a b ] [ 0 - 9 a - f ] { 3 } - [ 0 - 9 a - f ] { 12 } $ / i
188
187
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
+ }
190
193
}
191
194
Note . parseNoteIdAsync = function ( noteId ) {
192
195
return new Promise ( ( resolve , reject ) => {
@@ -199,7 +202,7 @@ module.exports = function (sequelize, DataTypes) {
199
202
} )
200
203
}
201
204
202
- async function syncNote ( noteInFS , note ) {
205
+ async function syncNote ( noteInFS , note ) {
203
206
const contentLength = noteInFS . content . length
204
207
205
208
let note2 = await note . update ( {
@@ -276,7 +279,11 @@ module.exports = function (sequelize, DataTypes) {
276
279
// try to parse note id by LZString Base64
277
280
try {
278
281
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
+ }
280
287
} catch ( err ) {
281
288
if ( err . message === 'Cannot read property \'charAt\' of undefined' ) {
282
289
logger . warning ( 'Looks like we can not decode "' + noteId + '" with LZString. Can be ignored.' )
@@ -290,7 +297,11 @@ module.exports = function (sequelize, DataTypes) {
290
297
// try to parse note id by base64url
291
298
try {
292
299
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
+ }
294
305
} catch ( err ) {
295
306
logger . error ( err )
296
307
return _callback ( null , null )
@@ -344,7 +355,9 @@ module.exports = function (sequelize, DataTypes) {
344
355
title = meta . title
345
356
} else {
346
357
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
+ }
348
361
}
349
362
if ( ! title ) title = 'Untitled'
350
363
return title
@@ -393,7 +406,9 @@ module.exports = function (sequelize, DataTypes) {
393
406
break
394
407
}
395
408
}
396
- if ( ! found ) { tags . push ( rawtags [ i ] ) }
409
+ if ( ! found ) {
410
+ tags . push ( rawtags [ i ] )
411
+ }
397
412
}
398
413
return tags
399
414
}
@@ -412,14 +427,26 @@ module.exports = function (sequelize, DataTypes) {
412
427
return obj
413
428
}
414
429
Note . parseMeta = function ( meta ) {
415
- var _meta = { }
430
+ var _meta : any = { }
416
431
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
+ }
423
450
}
424
451
return _meta
425
452
}
@@ -584,7 +611,7 @@ module.exports = function (sequelize, DataTypes) {
584
611
return operations
585
612
}
586
613
587
- function readFileSystemNote ( filePath ) {
614
+ function readFileSystemNote ( filePath ) {
588
615
const fsModifiedTime = moment ( fs . statSync ( filePath ) . mtime )
589
616
const content = fs . readFileSync ( filePath , 'utf8' )
590
617
@@ -595,7 +622,7 @@ module.exports = function (sequelize, DataTypes) {
595
622
}
596
623
}
597
624
598
- function shouldSyncNote ( note , noteInFS ) {
625
+ function shouldSyncNote ( note , noteInFS ) {
599
626
const dbModifiedTime = moment ( note . lastchangeAt || note . createdAt )
600
627
return noteInFS . lastchangeAt . isAfter ( dbModifiedTime ) && note . content !== noteInFS . content
601
628
}
0 commit comments