UNPKG

1.21 kBJavaScriptView Raw
1const unescape = require('unescape')
2
3const HEADING_REGEXP = /^<h1.*?>(.*?)<\/h1>/
4
5const titleize = string =>
6 string
7 .split(/\W+/gi)
8 .map(w => w.charAt(0).toUpperCase() + w.slice(1))
9 .join(' ')
10
11const dasherize = string =>
12 String(string).replace(/\W+/gi, '-')
13
14const lowercaseFirstChar = string =>
15 string.charAt(0).toLowerCase() + string.slice(1)
16
17const upcaseFirstChar = string =>
18 string.charAt(0).toUpperCase() + string.slice(1)
19
20const hasContent = content =>
21 !!content && content.replace(HEADING_REGEXP, '').trim().length > 0
22
23const 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
30const titleFromContentHeading = content => {
31 const [, title] = (content && content.match(HEADING_REGEXP)) || []
32
33 return title && unescape(title, 'all')
34}
35
36const crossPlatformPath = id =>
37 id.replace(/\\/g, '/')
38
39module.exports = {
40 crossPlatformPath,
41 dasherize,
42 titleize,
43 upcaseFirstChar,
44 lowercaseFirstChar,
45 hasContent,
46 replaceTemplateComments,
47 titleFromContentHeading
48}