This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
157 lines (133 loc) · 3.87 KB
/
index.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
var debug = require('debug')('metalsmith-hyphenate');
var extname = require('path').extname;
var p5 = require('parse5');
var Hypher = require('hypher');
var minimatch = require('minimatch');
/**
* Default Elements to hyphenate
*/
var DEFAULT_ELEMENTS = ['p', 'li', 'ol'];
/**
* Data attribute name
*/
var HYPHENATE_DATA_ATTR = 'data-hyphenate';
/**
* A Metalsmith plugin to add soft hyphens in HTML
*
* @param {Object} options (optional)
* @property {Array} [elements=['p', 'li', 'ol']] - HTML elements which needs to be hyphenated
* @property {Array} [langModule='hyphenation.en-us'] - Hypher language module(pattern) to use
* @property {String|Array} [ignore] - Glob expressions to ignore a file, or a set of files
* @return {Function}
*/
function plugin(options) {
options = options || {};
options.elements = options.elements || DEFAULT_ELEMENTS;
options.langModule = options.langModule || 'hyphenation.en-us';
if ((options.ignore !== undefined) &&
(Object.prototype.toString.call(options.ignore) === '[object String]')) {
options.ignore = [options.ignore];
}
debug('File ignore glob expressions: %j', options.ignore);
try {
var hypher = new Hypher(require(options.langModule));
} catch (err) {
console.log("Language module %s is not installed. Try 'npm install %s'",
options.langModule, options.langModule);
}
/**
* Check if a `value` is present in the array.
*
* @param {array} arr
* @param value
* @return {Boolean}
*/
function isPresent(arr, value) {
return arr.indexOf(value) < 0 ? false : true;
}
/**
* Check if a `file` is HTML.
*
* @param {String} file
* @return {Boolean}
*/
function isHtml(file) {
return /\.html?/.test(extname(file));
}
/**
* Traverses throught parsed DOM, and hyphenate text nodes
*
* @param {String} domString
* @return {String}
*/
function hyphenateText(dom, forceHyphenateAllChildren) {
if (dom.childNodes !== undefined) {
dom.childNodes.forEach(function(node) {
if (node.nodeName === '#text' && !isSkippable(node) && (forceHyphenateAllChildren || isPresent(options.elements, dom.tagName))) {
node.value = hypher.hyphenateText(node.value);
} else {
hyphenateText(node, isPresent(options.elements, dom.tagName));
}
});
}
return dom;
}
/**
* Check if the node is marked for skip
*
* @param {String} DOM node
* @return {Boolean}
*/
function isSkippable(node) {
var result = false;
if (node && node.parentNode && node.parentNode.attrs && Array.isArray(node.parentNode.attrs)) {
node.parentNode.attrs.forEach(function(attr) {
if (attr.name && attr.value && attr.name === HYPHENATE_DATA_ATTR && attr.value === 'false') {
result = true;
}
});
}
return result;
}
/**
* Check if the file matches the ignore glob patterns
*
* @param {String} file
* @return {Boolean}
*/
function isIgnoredFile(file) {
if (options.ignore !== undefined) {
var result = false;
options.ignore.forEach(function(pattern) {
if (minimatch(file, pattern)) {
result = true;
return false;
}
});
return result;
}
return false;
}
return function(files, metalsmith, done) {
setImmediate(done);
Object.keys(files).forEach(function(file) {
debug('checking if file matches ignore patterns: %s', file);
if (isIgnoredFile(file) === true) {
return;
}
debug('checking if it is an HTML file: %s', file);
if (isHtml(file) === false) {
return;
}
debug('hyphenating "%s"', file);
var dom = p5.parse(files[file].contents.toString());
dom = hyphenateText(dom);
files[file].contents = new Buffer(p5.serialize(dom));
});
};
}
/**
* Expose `plugin`.
*/
module.exports = plugin;