File: onsave-markdown_save.js

package info (click to toggle)
nixnote2 2.1.7~git20220718%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 11,984 kB
  • sloc: cpp: 78,958; java: 1,096; sh: 322; javascript: 107; ansic: 10; makefile: 6
file content (84 lines) | stat: -rw-r--r-- 2,666 bytes parent folder | download | duplicates (3)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*********************************************/
/* This is a sample note-save exit. It will  */
/* do very basic markdown editing when       */
/* saving a note.                            */
/*                                           */
/* These markdown tags are supported:        */
/* "---"  :  horizontal line.                */
/* "___"  :  horizontal line.                */
/* "***"  :  horizontal line.                */
/* "__"   :  italics                         */
/* "--"   :  Bold.                           */
/* "**"   :  Bold.                           */
/* "~~"   :  Strikeout.                      */
/* "[]"   :  insert a todo.                  */
/* "[x]"  :  insert a completed todo.        */
/* "[X]"  :  insert a completed todo.        */
/* "+"    :  Unordered list.                 */
/*********************************************/


/* This function converts tags with end tags (i.e. <b> or <i>) */
function markdown1(content, start, end, tag) {

   current = start;
   while (content.indexOf(tag)>0) {
    content = content.replace(tag,current);
        if (current === start) 
        current = end;
    else
        current = start;
   }
   // note.log(content);
   return content;
}




/* This function converts tags with no end (i.e. <hr>) */
function markdown2(content, html, tag) {

   while (content.indexOf(tag)>0) {
    content = content.replace(tag,html);
   }
   // note.log(content);
   return content;
}

/* This function converts "+" into an unordered list */
function markdownUl(content, tag) {
   var first = true;
   while (content.indexOf(tag)>0) {
      if (first)
         content = content.replace(tag, "<ul><li>");
      else
         content = content.replace(tag, "<li>");
      first = false;
   }
   return content; 
}

var c = note.getContents();

var todoUnchecked = "<input type=\"CHECKBOX\" onmouseover=\"style.cursor='hand'\" onclick=\"if(!checked) removeAttribute('checked'); else setAttribute('checked', 'checked'); editorWindow.editAlert();\" style=\"cursor: pointer;\">";
var todoChecked = "<input checked=\"checked\" type=\"checkbox\" onclick=\"if(!checked) removeAttribute('checked'); else setAttribute('checked', 'checked'); editorWindow.editAlert();\" style=\"cursor: hand;\">";


c = c.replace("[]", todoUnchecked);
c = c.replace("[x]", todoChecked);
c = c.replace("[X]", todoChecked);

c = markdown2(c, "<hr>", "---");
c = markdown2(c, "<hr>", "***");
c = markdown2(c, "<hr>", "___");


c = markdown1(c, "<b>","</b>", "--");
c = markdown1(c, "<b>","</b>", "**");
c = markdown1(c, "<i>","</i>", "__");
c = markdown1(c, "<del>","</del>", "~~");

c = markdownUl(c, "+");

note.setContents(c);