-
Notifications
You must be signed in to change notification settings - Fork 42
/
slide-properties.js
80 lines (61 loc) · 1.48 KB
/
slide-properties.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
"use strict";
/*
Markdown files can benefit from special metadata (~= frontMatter)
Each key value pair must be surrounded by dollar signs
Example:
$class:raw$
*/
var { merge } = require("lodash/fp");
module.exports = extractProps;
function extractProps (content) {
var props = {};
var lines = content.trim().split(/[\r\n]/);
while (addProp(props, lines[0])) {
lines.shift();
}
// data-has-notes?
props = merge(props, inferProps(content));
return Object.assign(props, {
"content": lines.join("\n")
});
}
// If content has notes, a dedicated prop can be useful for special CSS styling
function inferProps (content) {
var hasNotes = content.match(/[\r\n]note:/igm);
return addDataProp({}, "has-notes", Boolean(hasNotes));
}
function addProp (props, line) {
if (!line) {
return false;
}
line = line.trim();
var m = line.match(/^\$([a-z-]+):([^$]+?)\$$/i);
if (!m) {
return false;
}
var prop = m[1].toLowerCase().trim();
var val = m[2].trim();
switch (prop) {
case "class":
if (!props.classes) {
props.classes = [];
}
props.classes = props.classes.concat(val.split(/\s+/));
break;
case "id":
props.id = val;
break;
default:
addDataProp(props, prop, val);
break;
}
return true;
}
function addDataProp (props, prop, val) {
if (!props.datas) {
props.datas = {};
}
// cast because DOMString in the end
props.datas[prop] = String(val);
return props;
}