@@ -7,7 +7,7 @@ import base64url from "base64url";
7
7
import cheerio from "cheerio" ;
8
8
import * as fs from "fs" ;
9
9
import markdownIt from "markdown-it" ;
10
- import moment from "moment" ;
10
+ import moment , { Moment } from "moment" ;
11
11
12
12
// ot
13
13
import ot from "ot" ;
@@ -20,19 +20,24 @@ import config from "../config";
20
20
import { logger } from "../logger" ;
21
21
import { createNoteWithRevision , syncNote } from "../services/note" ;
22
22
import { stripTags } from "../string" ;
23
- import { MySequelize , NoteAttributes } from "./baseModel" ;
23
+ import { ModelObj , MySequelize , NoteAttributes , NoteMeta } from "./baseModel" ;
24
24
25
25
const md = markdownIt ( )
26
26
export const dmp = new DiffMatchPatch ( )
27
27
// permission types
28
28
const permissionTypes = [ 'freely' , 'editable' , 'limited' , 'locked' , 'protected' , 'private' ]
29
29
30
+ interface ParsedMeta {
31
+ markdown : string ,
32
+ meta : Record < string , string >
33
+ }
34
+
30
35
export class Note extends Model < NoteAttributes > implements NoteAttributes {
31
36
alias : string ;
32
37
authorship : string ;
33
38
content : string ;
34
39
id : string ;
35
- lastchangeAt : Date ;
40
+ lastchangeAt : Date | Moment ;
36
41
permission : string ;
37
42
savedAt : Date ;
38
43
shortid : string ;
@@ -107,7 +112,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
107
112
} )
108
113
109
114
Note . addHook ( 'beforeCreate' , function ( note : Note ) : Promise < void > {
110
- return new Promise ( function ( resolve , reject ) {
115
+ return new Promise ( function ( resolve ) {
111
116
// if no content specified then use default note
112
117
if ( ! note . content ) {
113
118
let filePath = config . defaultNotePath
@@ -124,8 +129,8 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
124
129
note . title = noteInFS . title
125
130
note . content = noteInFS . content
126
131
if ( filePath !== config . defaultNotePath ) {
127
- note . createdAt = noteInFS . lastchangeAt . toDate ( )
128
- note . lastchangeAt = noteInFS . lastchangeAt . toDate ( )
132
+ note . createdAt = ( noteInFS . lastchangeAt as Moment ) . toDate ( )
133
+ note . lastchangeAt = ( noteInFS . lastchangeAt as Moment ) . toDate ( )
129
134
}
130
135
}
131
136
}
@@ -142,7 +147,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
142
147
} )
143
148
}
144
149
145
- static associate ( models : any ) : void {
150
+ static associate ( models : ModelObj ) : void {
146
151
Note . belongsTo ( models . User , {
147
152
foreignKey : 'ownerId' ,
148
153
as : 'owner' ,
@@ -167,22 +172,22 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
167
172
}
168
173
169
174
170
- static checkFileExist ( filePath ) {
175
+ static checkFileExist ( filePath : string ) : boolean {
171
176
try {
172
177
return fs . statSync ( filePath ) . isFile ( )
173
178
} catch ( err ) {
174
179
return false
175
180
}
176
181
}
177
182
178
- static encodeNoteId ( id ) {
183
+ static encodeNoteId ( id : string ) : string {
179
184
// remove dashes in UUID and encode in url-safe base64
180
185
const str = id . replace ( / - / g, '' )
181
186
const hexStr = Buffer . from ( str , 'hex' )
182
187
return base64url . encode ( hexStr )
183
188
}
184
189
185
- static decodeNoteId ( encodedId ) {
190
+ static decodeNoteId ( encodedId : string ) : string {
186
191
// decode from url-safe base64
187
192
const id = base64url . toBuffer ( encodedId ) . toString ( 'hex' )
188
193
// add dashes between the UUID string parts
@@ -195,7 +200,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
195
200
return idParts . join ( '-' )
196
201
}
197
202
198
- static checkNoteIdValid ( id ) {
203
+ static checkNoteIdValid ( id : string ) : boolean {
199
204
const 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
200
205
const result = id . match ( uuidRegex )
201
206
if ( result && result . length === 1 ) {
@@ -205,7 +210,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
205
210
}
206
211
}
207
212
208
- static parseNoteIdAsync ( noteId ) {
213
+ static parseNoteIdAsync ( noteId : string ) : Promise < string > {
209
214
return new Promise ( ( resolve , reject ) => {
210
215
Note . parseNoteId ( noteId , ( err , id ) => {
211
216
if ( err ) {
@@ -216,7 +221,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
216
221
} )
217
222
}
218
223
219
- static parseNoteId ( noteId , callback ) {
224
+ static parseNoteId ( noteId : string , callback : ( err : Error | null , id : string ) => void ) : void {
220
225
async . series ( {
221
226
parseNoteIdByAlias : function ( _callback ) {
222
227
// try to parse note id by alias (e.g. doc)
@@ -320,7 +325,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
320
325
return _callback ( err , null )
321
326
}
322
327
}
323
- } , function ( err , result ) {
328
+ } , function ( err ) {
324
329
if ( err ) {
325
330
logger . error ( err )
326
331
return callback ( err , null )
@@ -329,7 +334,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
329
334
} )
330
335
}
331
336
332
- static parseNoteInfo ( body ) {
337
+ static parseNoteInfo ( body : string ) : Partial < NoteMeta > {
333
338
const parsed = Note . extractMeta ( body )
334
339
const $ = cheerio . load ( md . render ( parsed . markdown ) )
335
340
return {
@@ -338,13 +343,13 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
338
343
}
339
344
}
340
345
341
- static parseNoteTitle ( body ) {
346
+ static parseNoteTitle ( body : string ) : string {
342
347
const parsed = Note . extractMeta ( body )
343
348
const $ = cheerio . load ( md . render ( parsed . markdown ) )
344
349
return Note . extractNoteTitle ( parsed . meta , $ )
345
350
}
346
351
347
- static extractNoteTitle ( meta , $ ) {
352
+ static extractNoteTitle ( meta : Record < string , string > , $ : cheerio . Root ) : string {
348
353
let title = ''
349
354
if ( meta . title && ( typeof meta . title === 'string' || typeof meta . title === 'number' ) ) {
350
355
title = meta . title
@@ -358,20 +363,20 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
358
363
return title
359
364
}
360
365
361
- static generateDescription ( markdown ) {
366
+ static generateDescription ( markdown : string ) : string {
362
367
return markdown . substr ( 0 , 100 ) . replace ( / (?: \r \n | \r | \n ) / g, ' ' )
363
368
}
364
369
365
- static decodeTitle ( title ) {
370
+ static decodeTitle ( title : string ) : string {
366
371
return title || 'Untitled'
367
372
}
368
373
369
- static generateWebTitle ( title ) {
374
+ static generateWebTitle ( title : string ) : string {
370
375
title = ! title || title === 'Untitled' ? 'CodiMD - Collaborative markdown notes' : title + ' - CodiMD'
371
376
return title
372
377
}
373
378
374
- static extractNoteTags ( meta , $ ) {
379
+ static extractNoteTags ( meta : Record < string , string > , $ : cheerio . Root ) : string [ ] {
375
380
const tags = [ ]
376
381
const rawtags = [ ]
377
382
let metaTags
@@ -412,7 +417,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
412
417
return tags
413
418
}
414
419
415
- static extractMeta ( content ) {
420
+ static extractMeta ( content : string ) : ParsedMeta {
416
421
let obj = null
417
422
try {
418
423
obj = metaMarked ( content )
@@ -427,8 +432,8 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
427
432
return obj
428
433
}
429
434
430
- static parseMeta ( meta ) {
431
- const _meta : any = { }
435
+ static parseMeta ( meta : Record < string , string > ) : Partial < NoteMeta > {
436
+ const _meta : Partial < NoteMeta > = { }
432
437
if ( meta ) {
433
438
if ( meta . title && ( typeof meta . title === 'string' || typeof meta . title === 'number' ) ) {
434
439
_meta . title = meta . title
@@ -452,7 +457,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
452
457
return _meta
453
458
}
454
459
455
- static updateAuthorshipByOperation ( operation , userId , authorships ) {
460
+ static updateAuthorshipByOperation ( operation : any , userId : string , authorships : any ) : any {
456
461
let index = 0
457
462
const timestamp = Date . now ( )
458
463
for ( let i = 0 ; i < operation . length ; i ++ ) {
@@ -554,7 +559,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
554
559
return authorships
555
560
}
556
561
557
- static transformPatchToOperations ( patch , contentLength ) {
562
+ static transformPatchToOperations ( patch : any , contentLength : number ) {
558
563
const operations = [ ]
559
564
if ( patch . length > 0 ) {
560
565
// calculate original content length
@@ -615,7 +620,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
615
620
}
616
621
}
617
622
618
- function readFileSystemNote ( filePath ) {
623
+ function readFileSystemNote ( filePath : string ) : Partial < Note > {
619
624
const fsModifiedTime = moment ( fs . statSync ( filePath ) . mtime )
620
625
const content = fs . readFileSync ( filePath , 'utf8' )
621
626
0 commit comments