1 | const unescape = require('unescape')
|
2 |
|
3 | const HEADING_REGEXP = /^<h1.*?>(.*?)<\/h1>/
|
4 |
|
5 | const titleize = string =>
|
6 | string
|
7 | .split(/\W+/gi)
|
8 | .map(w => w.charAt(0).toUpperCase() + w.slice(1))
|
9 | .join(' ')
|
10 |
|
11 | const dasherize = string =>
|
12 | String(string).replace(/\W+/gi, '-')
|
13 |
|
14 | const lowercaseFirstChar = string =>
|
15 | string.charAt(0).toLowerCase() + string.slice(1)
|
16 |
|
17 | const upcaseFirstChar = string =>
|
18 | string.charAt(0).toUpperCase() + string.slice(1)
|
19 |
|
20 | const hasContent = content =>
|
21 | !!content && content.replace(HEADING_REGEXP, '').trim().length > 0
|
22 |
|
23 | const replaceTemplateComments = (html, marks) =>
|
24 | Object.keys(marks).reduce((current, key) => {
|
25 | const regexp = new RegExp(`<!--\\s?uiengine:${key}\\s?-->`, 'gi')
|
26 | const content = marks[key]
|
27 | return current.replace(regexp, content || '')
|
28 | }, html)
|
29 |
|
30 | const titleFromContentHeading = content => {
|
31 | const [, title] = (content && content.match(HEADING_REGEXP)) || []
|
32 |
|
33 | return title && unescape(title, 'all')
|
34 | }
|
35 |
|
36 | const crossPlatformPath = id =>
|
37 | id.replace(/\\/g, '/')
|
38 |
|
39 | module.exports = {
|
40 | crossPlatformPath,
|
41 | dasherize,
|
42 | titleize,
|
43 | upcaseFirstChar,
|
44 | lowercaseFirstChar,
|
45 | hasContent,
|
46 | replaceTemplateComments,
|
47 | titleFromContentHeading
|
48 | }
|