summaryrefslogtreecommitdiff
path: root/lib/note.js
blob: 1212e1a6c44dd98a872d009e9544f225fc6bfac8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//note
//external modules
var LZString = require('lz-string');
var marked = require('marked');
var cheerio = require('cheerio');

//others
var db = require("./db.js");

//public
var note = {
    checkNoteIdValid: checkNoteIdValid,
    checkNoteExist: checkNoteExist,
    getNoteTitle: getNoteTitle
};

function checkNoteIdValid(noteId) {
    try {
        //console.log(noteId);
        var id = LZString.decompressFromBase64(noteId);
        if (!id) return false;
        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;
        var result = id.match(uuidRegex);
        if (result && result.length == 1)
            return true;
        else
            return false;
    } catch (err) {
        console.error(err);
        return false;
    }
}

function checkNoteExist(noteId) {
    try {
        //console.log(noteId);
        var id = LZString.decompressFromBase64(noteId);
        db.readFromDB(id, function (err, result) {
            if (err) return false;
            return true;
        });
    } catch (err) {
        console.error(err);
        return false;
    }
}

//get title
function getNoteTitle(body) {
    var $ = cheerio.load(marked(body));
    var h1s = $("h1");
    var title = "";
    if (h1s.length > 0)
        title = h1s.first().text();
    else
        title = "Untitled";
    return title;
}

module.exports = note;