UNPKG

2.9 kBJavaScriptView Raw
1'use strict';
2
3const chalk = require('chalk');
4
5module.exports = {
6 /**
7 * Has ignored text?
8 *
9 * @param {string} text
10 * @returns {boolean}
11 */
12 hasIgnoredText(text) {
13 return text.search(/yaspeller\s+ignore/) !== -1;
14 },
15 /**
16 * Ignore lines.
17 *
18 * @param {string} text
19 * @returns {text}
20 */
21 lines(text) {
22 return text
23 .replace(/^.*?\/\/\s*yaspeller\s+ignore\s*$/mg, '')
24 .replace(/^.*?<!--\s*yaspeller\s+ignore\s*-->.*?$/mg, '')
25 .replace(/^.*?\/\*\s*yaspeller\s+ignore\s*\*\/.*?$/mg, '');
26 },
27 /**
28 * Ignore blocks.
29 *
30 * @param {string} text
31 * @returns {text}
32 */
33 blocks(text) {
34 return text
35 .replace(/\/\*\s*yaspeller\s+ignore:start\s*\*\/[^]*?\/\*\s*yaspeller\s+ignore:end\s*\*\//g, '')
36 .replace(/<!--\s*yaspeller\s+ignore:start\s*-->[^]*?<!--\s*yaspeller\s+ignore:end\s*-->/g, '')
37 .replace(/\/\/\s*yaspeller\s+ignore:start[^]*?\/\/\s*yaspeller\s+ignore:end.*?(\r?\n|$)/g, '');
38 },
39 /**
40 * Ignore HTML comments.
41 *
42 * @param {string} text
43 * @returns {text}
44 */
45 comments(text) {
46 const comments = [
47 ['<!--', '-->'],
48 ['<!ENTITY', '>'],
49 ['<!DOCTYPE', '>'],
50 ['<\\?xml', '\\?>'],
51 ['<!\\[CDATA\\[', '\\]\\]>']
52 ];
53
54 comments.forEach(function(tag) {
55 const re = new RegExp(tag[0] + '[^]*?' + tag[1], 'gi');
56 text = text.replace(re, ' ');
57 });
58
59 return text;
60 },
61 /**
62 * Ignore tags.
63 *
64 * @param {string} text
65 * @param {Array} tags
66 * @returns {text}
67 */
68 tags(text, tags) {
69 const bufTags = [];
70 tags.forEach(function(tag) {
71 bufTags.push(['<' + tag + '(\\s[^>]*?)?>', '</' + tag + '>']);
72 }, this);
73
74 bufTags.forEach(function(tag) {
75 const re = new RegExp(tag[0] + '[^]*?' + tag[1], 'gi');
76 text = text.replace(re, ' ');
77 });
78
79 return text;
80 },
81 /**
82 * Prepares regular expressions to remove text.
83 *
84 * @param {Array|undefined} data
85 *
86 * @returns {Array}
87 */
88 prepareRegExpToIgnoreText(data) {
89 const result = [];
90
91 if (typeof data === 'string') {
92 data = [data];
93 }
94
95 Array.isArray(data) && data.forEach(function(re) {
96 try {
97 if (typeof re === 'string') {
98 result.push(new RegExp(re, 'g'));
99 }
100
101 if (Array.isArray(re)) {
102 result.push(new RegExp(re[0], typeof re[1] === 'undefined' ? 'g' : re[1]));
103 }
104 } catch (e) {
105 console.error(chalk.red('Error in RegExp "' + re.toString() + '": ' + e));
106 }
107 });
108
109 return result;
110 }
111};