|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | angular.module('neContentEditors',[]) |
8 | | -.service('neMarkdown', ['$document','NeRemarked', 'neMarked', function($document, ReMarked, marked){ |
| 8 | +.factory('neMarkdown', ['$document','NeRemarked', 'neMarked', function($document, ReMarked, marked){ |
| 9 | + |
| 10 | + var md = {}; // markdown instance |
| 11 | + |
9 | 12 | var reMarkedOptions = { |
10 | | - link_list:false, // render links as references, create link list as appendix |
11 | | - h1_setext:false, // underline h1 headers |
12 | | - h2_setext:false, // underline h2 headers |
13 | | - h_atx_suf:false, // header suffixes (###) |
14 | | - gfm_code:false, // gfm code blocks (```) |
15 | | - li_bullet:"*", // list item bullet style |
16 | | - hr_char:"-", // hr style |
17 | | - indnt_str:" ", // indentation string |
18 | | - bold_char:"*", // char used for strong |
19 | | - emph_char:"_", // char used for em |
20 | | - gfm_del:true, // ~~strikeout~~ for <del>strikeout</del> |
21 | | - gfm_tbls:true, // markdown-extra tables |
22 | | - tbl_edges:false, // show side edges on tables |
23 | | - hash_lnks:false, // anchors w/hash hrefs as links |
24 | | - br_only:false // avoid using " " as line break indicator |
| 13 | + link_list:false, // render links as references, create link list as appendix |
| 14 | + h1_setext:false, // underline h1 headers |
| 15 | + h2_setext:false, // underline h2 headers |
| 16 | + h_atx_suf:false, // header suffixes (###) |
| 17 | + gfm_code:false, // gfm code blocks (```) |
| 18 | + li_bullet:"*", // list item bullet style |
| 19 | + hr_char:"-", // hr style |
| 20 | + indnt_str:" ", // indentation string |
| 21 | + bold_char:"*", // char used for strong |
| 22 | + emph_char:"_", // char used for em |
| 23 | + gfm_del:true, // ~~strikeout~~ for <del>strikeout</del> |
| 24 | + gfm_tbls:true, // markdown-extra tables |
| 25 | + tbl_edges:false, // show side edges on tables |
| 26 | + hash_lnks:false, // anchors w/hash hrefs as links |
| 27 | + br_only:false // avoid using " " as line break indicator |
25 | 28 | }; |
26 | 29 | var reMarker = new ReMarked(reMarkedOptions); |
27 | 30 |
|
28 | | - this.parseHTML = function(htmlString){ |
29 | | - return reMarker.render(htmlString || ''); |
| 31 | + md.parseHTML = function(htmlString){ |
| 32 | + return reMarker.render(htmlString || ''); |
30 | 33 | }; |
31 | 34 |
|
32 | 35 | var markedOptions = { |
33 | | - gfm: true, |
34 | | - tables: true, |
35 | | - breaks: false, |
36 | | - pedantic: false, |
37 | | - sanitize: false, |
38 | | - smartLists: true, |
39 | | - smartypants: false |
| 36 | + gfm: true, |
| 37 | + tables: true, |
| 38 | + breaks: false, |
| 39 | + pedantic: false, |
| 40 | + sanitize: false, |
| 41 | + smartLists: true, |
| 42 | + smartypants: false |
40 | 43 | }; |
41 | 44 | marked.setOptions(markedOptions); |
42 | 45 |
|
43 | | - this.renderHTML = function(mdString){ |
| 46 | + |
| 47 | + md.renderHTML = function(mdString){ |
44 | 48 | return marked(mdString || ''); |
45 | 49 | }; |
46 | 50 |
|
@@ -82,74 +86,74 @@ angular.module('neContentEditors',[]) |
82 | 86 | } |
83 | 87 |
|
84 | 88 |
|
85 | | - this.editor = { |
86 | | - // TODO: undo, redo |
87 | | - //undo: formatDoc('undo'), |
88 | | - //redo: formatDoc('redo'), |
89 | | - |
90 | | - bold: wrapText('**','**','bold text'), |
91 | | - italic: wrapText('*','*','italic text'), |
92 | | - strikethrough: wrapText('~~','~~','strikethrough text'), |
93 | | - h1: wrapText('\n# ','','headline 1'), |
94 | | - h2: wrapText('\n## ','','headline 2'), |
95 | | - h3: wrapText('\n### ','','headline 3'), |
96 | | - h4: wrapText('\n#### ','','headline 4'), |
97 | | - h5: wrapText('\n##### ','','headline 5'), |
98 | | - h6: wrapText('\n###### ','','headline 6'), |
99 | | - ol: wrapText('\n1. ','','numbered list'), |
100 | | - ul: wrapText('\n- ','','bulleted list'), |
101 | | - indent: wrapText(' ','','','replace'), |
102 | | - dedent: wrapText(' ','','','remove'), |
103 | | - blocquote: wrapText('\n> ','','blocquote text'), |
104 | | - hr: wrapText('\n\n-----\n\n','','','replace'), |
105 | | - |
106 | | - link: function(selection, url, name){ // selection, [url || usePrompt] |
| 89 | + md.editor = { |
| 90 | + // TODO: undo, redo |
| 91 | + //undo: formatDoc('undo'), |
| 92 | + //redo: formatDoc('redo'), |
| 93 | + |
| 94 | + bold: wrapText('**','**','bold text'), |
| 95 | + italic: wrapText('*','*','italic text'), |
| 96 | + strikethrough: wrapText('~~','~~','strikethrough text'), |
| 97 | + h1: wrapText('\n# ','','headline 1'), |
| 98 | + h2: wrapText('\n## ','','headline 2'), |
| 99 | + h3: wrapText('\n### ','','headline 3'), |
| 100 | + h4: wrapText('\n#### ','','headline 4'), |
| 101 | + h5: wrapText('\n##### ','','headline 5'), |
| 102 | + h6: wrapText('\n###### ','','headline 6'), |
| 103 | + ol: wrapText('\n1. ','','numbered list'), |
| 104 | + ul: wrapText('\n- ','','bulleted list'), |
| 105 | + indent: wrapText(' ','','','replace'), |
| 106 | + dedent: wrapText(' ','','','remove'), |
| 107 | + blocquote: wrapText('\n> ','','blocquote text'), |
| 108 | + hr: wrapText('\n\n-----\n\n','','','replace'), |
| 109 | + |
| 110 | + link: function(selection, url, name){ // selection, [url || usePrompt] |
107 | 111 | if(!(url || url==='')){ |
108 | | - url = prompt('Please enter link url','http://'); |
| 112 | + url = prompt('Please enter link url','http://'); |
109 | 113 | } |
110 | 114 | if(url) return wrapText('', '[ ' +(name || 'link text')+ ' ]( ' +url+ ' )' ,'','replace')(selection); |
111 | 115 | else return ''; |
112 | | - }, |
113 | | - |
114 | | - image: function(selection, url){ // selection, [url || usePrompt] |
115 | | - if(!(url || url==='')){ |
116 | | - url = prompt('Please enter image url','http://'); |
117 | | - } |
118 | | - if(url) return wrapText('', '' ,'','replace')(selection); |
119 | | - else return ''; |
120 | | - }, |
121 | | - table: function(selection, cols, rows){ |
122 | | - cols = parseInt(cols, 10); |
123 | | - rows = parseInt(rows, 10); |
124 | | - if(cols>0 && rows>0) return wrapText('', tableMD(cols, rows),'','add')(selection); |
125 | | - else return selection.inputValue; |
126 | | - } |
| 116 | + }, |
| 117 | + |
| 118 | + image: function(selection, url){ // selection, [url || usePrompt] |
| 119 | + if(!(url || url==='')){ |
| 120 | + url = prompt('Please enter image url','http://'); |
| 121 | + } |
| 122 | + if(url) return wrapText('', '' ,'','replace')(selection); |
| 123 | + else return ''; |
| 124 | + }, |
| 125 | + table: function(selection, cols, rows){ |
| 126 | + cols = parseInt(cols, 10); |
| 127 | + rows = parseInt(rows, 10); |
| 128 | + if(cols>0 && rows>0) return wrapText('', tableMD(cols, rows),'','add')(selection); |
| 129 | + else return selection.inputValue; |
| 130 | + } |
127 | 131 | }; |
128 | 132 |
|
129 | 133 | function tableMD(cols, rows){ |
130 | | - var mdTable = '\n'; |
131 | | - |
132 | | - for(var r=1;r<rows+3;r++){ |
133 | | - for(var c=1;c<cols+1;c++){ |
134 | | - if(c===1) mdTable+='\n'; |
135 | | - else mdTable+=' | '; |
136 | | - |
137 | | - if(r===1) mdTable+='col '+c; |
138 | | - else if(r===2) mdTable+='-----'; |
139 | | - else mdTable+='row '+(r-2); |
140 | | - } |
141 | | - } |
142 | | - |
143 | | - //return '\n\ncol 1 | col 2 | col 3' + |
144 | | - //'\n----- | ----- | -----' + |
145 | | - //'\nrow 1 | row 1 | row 1' + |
146 | | - //'\nrow 2 | row 2 | row 2' + |
147 | | - //'\nrow 3 | row 3 | row 3\n'; |
148 | | - |
149 | | - return mdTable+'\n'; |
| 134 | + var mdTable = '\n'; |
| 135 | + |
| 136 | + for(var r=1;r<rows+3;r++){ |
| 137 | + for(var c=1;c<cols+1;c++){ |
| 138 | + if(c===1) mdTable+='\n'; |
| 139 | + else mdTable+=' | '; |
| 140 | + |
| 141 | + if(r===1) mdTable+='col '+c; |
| 142 | + else if(r===2) mdTable+='-----'; |
| 143 | + else mdTable+='row '+(r-2); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + //return '\n\ncol 1 | col 2 | col 3' + |
| 148 | + //'\n----- | ----- | -----' + |
| 149 | + //'\nrow 1 | row 1 | row 1' + |
| 150 | + //'\nrow 2 | row 2 | row 2' + |
| 151 | + //'\nrow 3 | row 3 | row 3\n'; |
| 152 | + |
| 153 | + return mdTable+'\n'; |
150 | 154 | } |
151 | 155 |
|
152 | | - return this; |
| 156 | + return md; |
153 | 157 | }]) |
154 | 158 | .controller('NeMdCtrl', ['$scope', 'neMarkdown', function($scope, markdown){ |
155 | 159 | $scope.editor = markdown.editor; |
|
0 commit comments