@@ -7,7 +7,7 @@ import base64url from "base64url";
77import cheerio from "cheerio" ;
88import * as fs from "fs" ;
99import markdownIt from "markdown-it" ;
10- import moment from "moment" ;
10+ import moment , { Moment } from "moment" ;
1111
1212// ot
1313import ot from "ot" ;
@@ -20,19 +20,24 @@ import config from "../config";
2020import { logger } from "../logger" ;
2121import { createNoteWithRevision , syncNote } from "../services/note" ;
2222import { stripTags } from "../string" ;
23- import { MySequelize , NoteAttributes } from "./baseModel" ;
23+ import { ModelObj , MySequelize , NoteAttributes , NoteMeta } from "./baseModel" ;
2424
2525const md = markdownIt ( )
2626export const dmp = new DiffMatchPatch ( )
2727// permission types
2828const permissionTypes = [ 'freely' , 'editable' , 'limited' , 'locked' , 'protected' , 'private' ]
2929
30+ interface ParsedMeta {
31+ markdown : string ,
32+ meta : Record < string , string >
33+ }
34+
3035export class Note extends Model < NoteAttributes > implements NoteAttributes {
3136 alias : string ;
3237 authorship : string ;
3338 content : string ;
3439 id : string ;
35- lastchangeAt : Date ;
40+ lastchangeAt : Date | Moment ;
3641 permission : string ;
3742 savedAt : Date ;
3843 shortid : string ;
@@ -107,7 +112,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
107112 } )
108113
109114 Note . addHook ( 'beforeCreate' , function ( note : Note ) : Promise < void > {
110- return new Promise ( function ( resolve , reject ) {
115+ return new Promise ( function ( resolve ) {
111116 // if no content specified then use default note
112117 if ( ! note . content ) {
113118 let filePath = config . defaultNotePath
@@ -124,8 +129,8 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
124129 note . title = noteInFS . title
125130 note . content = noteInFS . content
126131 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 ( )
129134 }
130135 }
131136 }
@@ -142,7 +147,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
142147 } )
143148 }
144149
145- static associate ( models : any ) : void {
150+ static associate ( models : ModelObj ) : void {
146151 Note . belongsTo ( models . User , {
147152 foreignKey : 'ownerId' ,
148153 as : 'owner' ,
@@ -167,22 +172,22 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
167172 }
168173
169174
170- static checkFileExist ( filePath ) {
175+ static checkFileExist ( filePath : string ) : boolean {
171176 try {
172177 return fs . statSync ( filePath ) . isFile ( )
173178 } catch ( err ) {
174179 return false
175180 }
176181 }
177182
178- static encodeNoteId ( id ) {
183+ static encodeNoteId ( id : string ) : string {
179184 // remove dashes in UUID and encode in url-safe base64
180185 const str = id . replace ( / - / g, '' )
181186 const hexStr = Buffer . from ( str , 'hex' )
182187 return base64url . encode ( hexStr )
183188 }
184189
185- static decodeNoteId ( encodedId ) {
190+ static decodeNoteId ( encodedId : string ) : string {
186191 // decode from url-safe base64
187192 const id = base64url . toBuffer ( encodedId ) . toString ( 'hex' )
188193 // add dashes between the UUID string parts
@@ -195,7 +200,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
195200 return idParts . join ( '-' )
196201 }
197202
198- static checkNoteIdValid ( id ) {
203+ static checkNoteIdValid ( id : string ) : boolean {
199204 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
200205 const result = id . match ( uuidRegex )
201206 if ( result && result . length === 1 ) {
@@ -205,7 +210,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
205210 }
206211 }
207212
208- static parseNoteIdAsync ( noteId ) {
213+ static parseNoteIdAsync ( noteId : string ) : Promise < string > {
209214 return new Promise ( ( resolve , reject ) => {
210215 Note . parseNoteId ( noteId , ( err , id ) => {
211216 if ( err ) {
@@ -216,7 +221,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
216221 } )
217222 }
218223
219- static parseNoteId ( noteId , callback ) {
224+ static parseNoteId ( noteId : string , callback : ( err : Error | null , id : string ) => void ) : void {
220225 async . series ( {
221226 parseNoteIdByAlias : function ( _callback ) {
222227 // try to parse note id by alias (e.g. doc)
@@ -320,7 +325,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
320325 return _callback ( err , null )
321326 }
322327 }
323- } , function ( err , result ) {
328+ } , function ( err ) {
324329 if ( err ) {
325330 logger . error ( err )
326331 return callback ( err , null )
@@ -329,7 +334,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
329334 } )
330335 }
331336
332- static parseNoteInfo ( body ) {
337+ static parseNoteInfo ( body : string ) : Partial < NoteMeta > {
333338 const parsed = Note . extractMeta ( body )
334339 const $ = cheerio . load ( md . render ( parsed . markdown ) )
335340 return {
@@ -338,13 +343,13 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
338343 }
339344 }
340345
341- static parseNoteTitle ( body ) {
346+ static parseNoteTitle ( body : string ) : string {
342347 const parsed = Note . extractMeta ( body )
343348 const $ = cheerio . load ( md . render ( parsed . markdown ) )
344349 return Note . extractNoteTitle ( parsed . meta , $ )
345350 }
346351
347- static extractNoteTitle ( meta , $ ) {
352+ static extractNoteTitle ( meta : Record < string , string > , $ : cheerio . Root ) : string {
348353 let title = ''
349354 if ( meta . title && ( typeof meta . title === 'string' || typeof meta . title === 'number' ) ) {
350355 title = meta . title
@@ -358,20 +363,20 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
358363 return title
359364 }
360365
361- static generateDescription ( markdown ) {
366+ static generateDescription ( markdown : string ) : string {
362367 return markdown . substr ( 0 , 100 ) . replace ( / (?: \r \n | \r | \n ) / g, ' ' )
363368 }
364369
365- static decodeTitle ( title ) {
370+ static decodeTitle ( title : string ) : string {
366371 return title || 'Untitled'
367372 }
368373
369- static generateWebTitle ( title ) {
374+ static generateWebTitle ( title : string ) : string {
370375 title = ! title || title === 'Untitled' ? 'CodiMD - Collaborative markdown notes' : title + ' - CodiMD'
371376 return title
372377 }
373378
374- static extractNoteTags ( meta , $ ) {
379+ static extractNoteTags ( meta : Record < string , string > , $ : cheerio . Root ) : string [ ] {
375380 const tags = [ ]
376381 const rawtags = [ ]
377382 let metaTags
@@ -412,7 +417,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
412417 return tags
413418 }
414419
415- static extractMeta ( content ) {
420+ static extractMeta ( content : string ) : ParsedMeta {
416421 let obj = null
417422 try {
418423 obj = metaMarked ( content )
@@ -427,8 +432,8 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
427432 return obj
428433 }
429434
430- static parseMeta ( meta ) {
431- const _meta : any = { }
435+ static parseMeta ( meta : Record < string , string > ) : Partial < NoteMeta > {
436+ const _meta : Partial < NoteMeta > = { }
432437 if ( meta ) {
433438 if ( meta . title && ( typeof meta . title === 'string' || typeof meta . title === 'number' ) ) {
434439 _meta . title = meta . title
@@ -452,7 +457,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
452457 return _meta
453458 }
454459
455- static updateAuthorshipByOperation ( operation , userId , authorships ) {
460+ static updateAuthorshipByOperation ( operation : any , userId : string , authorships : any ) : any {
456461 let index = 0
457462 const timestamp = Date . now ( )
458463 for ( let i = 0 ; i < operation . length ; i ++ ) {
@@ -554,7 +559,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
554559 return authorships
555560 }
556561
557- static transformPatchToOperations ( patch , contentLength ) {
562+ static transformPatchToOperations ( patch : any , contentLength : number ) {
558563 const operations = [ ]
559564 if ( patch . length > 0 ) {
560565 // calculate original content length
@@ -615,7 +620,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
615620 }
616621}
617622
618- function readFileSystemNote ( filePath ) {
623+ function readFileSystemNote ( filePath : string ) : Partial < Note > {
619624 const fsModifiedTime = moment ( fs . statSync ( filePath ) . mtime )
620625 const content = fs . readFileSync ( filePath , 'utf8' )
621626
0 commit comments