1 | 'use strict';
|
2 |
|
3 | module.exports = {
|
4 | |
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | isHTML(text) {
|
11 | return text.search(/<[a-z!]/i) !== -1;
|
12 | },
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | isMarkdown(text) {
|
20 | return [
|
21 | /^===/m,
|
22 | /^\s{0,5}```/m,
|
23 | /-- ?:?\|/,
|
24 | /\)\[(https?|mailto):/
|
25 | ].some(el => text.search(el) !== -1);
|
26 | },
|
27 | /**
|
28 | * Get format.
|
29 | *
|
30 | * @param {string} text
|
31 | * @param {Object} settings
|
32 | * @returns {string}
|
33 | */
|
34 | getFormat(text, settings) {
|
35 | const format = settings.format;
|
36 | const extname = (settings.extname || '').toLowerCase();
|
37 | const extnames = {
|
38 | '.htm': 'html',
|
39 | '.html': 'html',
|
40 | '.xhtml': 'html',
|
41 | '.xml': 'html',
|
42 | '.svg': 'html',
|
43 | '.markdown': 'markdown',
|
44 | '.md': 'markdown'
|
45 | };
|
46 |
|
47 | if (['html', 'markdown', 'plain'].indexOf(format) !== -1) {
|
48 | return format;
|
49 | }
|
50 |
|
51 | if (format === 'auto' || !format) {
|
52 | if (extnames[extname]) {
|
53 | return extnames[extname];
|
54 | }
|
55 |
|
56 | if (this.isMarkdown(text)) {
|
57 | return 'markdown';
|
58 | } else if (this.isHTML(text)) {
|
59 | return 'html';
|
60 | }
|
61 | }
|
62 |
|
63 | return 'plain';
|
64 | },
|
65 | /**
|
66 | * Get API format.
|
67 | *
|
68 | * @param {string} format
|
69 | * @returns {string}
|
70 | */
|
71 | getApiFormat(format) {
|
72 | return format === 'html' || format === 'markdown' ? 'html' : 'plain';
|
73 | }
|
74 | };
|
75 |
|
\ | No newline at end of file |