-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
174 lines (148 loc) · 5.45 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Copyright 2019 David Herron
*
* This file is part of AkashaCMS-embeddables (http://akashacms.com/).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const util = require('util');
const path = require('path');
const akasha = require('akasharender');
const mahabhuta = akasha.mahabhuta;
const pluginName = "@akashacms/plugin-authors";
const _plugin_config = Symbol('config');
const _plugin_options = Symbol('options');
module.exports = class AuthorsPlugin extends akasha.Plugin {
constructor() { super(pluginName); }
configure(config, options) {
this[_plugin_config] = config;
this[_plugin_options] = options;
options.config = config;
config.addPartialsDir(path.join(__dirname, 'partials'));
config.addMahabhuta(module.exports.mahabhutaArray(options));
if (!options.authors) options.authors = [];
}
get config() { return this[_plugin_config]; }
get options() { return this[_plugin_options]; }
};
function findAuthor(options, author) {
if (author === "default") {
author = options.default;
}
if (typeof author !== 'string') {
throw new Error(`findAuthor supplied author must be a string value ${util.inspect(author)}`);
}
for (let aut of options.authors) {
if (isAuthor(aut) && aut.code === author) {
return aut;
}
}
return undefined;
}
// Type Guard
function isAuthor(author) {
if (typeof author !== 'object') {
throw new Error(`isAuthor - author object must be object ${util.inspect(author)}`);
}
if (typeof author.fullname !== 'string'
|| typeof author.url !== 'string') {
throw new Error(`isAuthor - invalid author object ${util.inspect(author)}`);
}
return true;
}
const getAuthors = (options, $element, metadata) => {
let authors;
if ($element.data('authors')) {
authors = $element.data('authors');
// console.log(`getAuthors data authors ${util.inspect(authors)}`);
} else if (metadata.authors) {
authors = metadata.authors;
} else {
authors = [ "default" ];
}
if (typeof authors === 'string') authors = [ authors ];
if (!Array.isArray(authors)) {
throw new Error(`getAuthors invalid author object ${util.inspect(author)}`);
}
// console.log(`getAuthors looking for ${util.inspect(authors)}`);
let authorList = [];
for (let aut of authors) {
let found = findAuthor(options, aut);
if (!found) {
throw new Error(`getAuthors did not find author ${util.inspect(aut)}`);
}
authorList.push(found);
}
// console.log(`getAuthors found ${util.inspect(authorList)}`);
return authorList;
};
const getID = ($element) => {
return $element.attr('id')
? $element.attr('id')
: undefined;
};
const getAdditionalClasses = ($element) => {
return $element.attr('class')
? $element.attr('class')
: undefined;
};
const getStyle = ($element) => {
return $element.attr('style')
? $element.attr('style')
: undefined;
};
const getTemplate = ($element, _default) => {
return $element.attr("template")
? $element.attr("template")
: _default;
};
module.exports.process = async function(text, metadata, options) {
let funcs = module.exports.mahabhutaArray(options);
// console.log(`process received metadata ${util.inspect(metadata)}`);
// console.log(`process received funcs ${util.inspect(funcs)}`);
let ret = await mahabhuta.processAsync(text, metadata, funcs);
// console.log(`process returning ${ret}`);
return ret;
};
module.exports.mahabhutaArray = function(options) {
let ret = new mahabhuta.MahafuncArray(pluginName, options);
ret.addMahafunc(new AuthorBylineElement());
ret.addMahafunc(new AuthorBioElement());
return ret;
};
class AuthorBylineElement extends mahabhuta.CustomElement {
get elementName() { return "authors-byline"; }
async process($element, metadata, dirty) {
/* TODO for an author thumbnail they are to pass in an href */
// console.log(`AuthorBylineElement ${util.inspect(metadata)}`);
return akasha.partial(this.array.options.config,
getTemplate($element, "authors-byline.html.ejs"), {
id: getID($element),
style: getStyle($element),
additionalClasses: getAdditionalClasses($element),
authors: getAuthors(this.array.options, $element, metadata)
});
}
}
class AuthorBioElement extends mahabhuta.CustomElement {
get elementName() { return "authors-bio-block"; }
async process($element, metadata, dirty) {
return akasha.partial(this.array.options.config,
getTemplate($element, "authors-bio-block.html.ejs"), {
id: getID($element),
style: getStyle($element),
additionalClasses: getAdditionalClasses($element),
authors: getAuthors(this.array.options, $element, metadata)
});
}
}