Skip to content

Commit

Permalink
Fix after testing with akashacms-example
Browse files Browse the repository at this point in the history
  • Loading branch information
robogeek committed Aug 11, 2019
1 parent 879e3c7 commit d182653
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 67 deletions.
37 changes: 31 additions & 6 deletions guide/index.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Add the following to `package.json`
```json
"dependencies": {
...
"@akashacms/plugin-author": ">=0.7"
"@akashacms/plugins-author": ">=0.7"
...
}
```
Expand All @@ -42,12 +42,14 @@ config
{
code: "boygeorge",
fullname: "Boy George",
url: "URL"
url: "URL",
bio: "<p>Weird ass british rocker</p>"
},
{
code: "eltonjohn",
fullname: "Elton John",
url: "URL"
url: "URL",
bio: "<p>Mainstream british rocker</p>"
}
]
})
Expand All @@ -57,14 +59,14 @@ config

# Custom Tags

## `author-byline`
## `authors-byline`

```html
<author-byline data-authors='[ "authorcode" ]'></author-byline>
<authors-byline data-authors='[ "authorcode" ]'></author-byline>

OR

<author-byline data-authors="authorcode"></author-byline>
<authors-byline data-authors="authorcode"></author-byline>
```

The other attributes are:
Expand All @@ -81,4 +83,27 @@ The data provided is:
* `additionalClasses` -- Any class value supplied, or `undefined`
* `authors` -- The author objects corresponding to the supplied authorcode(s)

## `authors-bio-block`

```html
<authors-bio-block data-authors="eltonjohn"></authors-bio-block>

OR

<authors-bio-block data-authors='[ "boygeorge", "eltonjohn" ]'></authors-bio-block>
```

The other attributes are:

* `template="partial-file.html.ejs"` -- Override the default template, `authors-bio-block.html.ejs`
* `id="ID"` -- An ID attribute for the outer element
* `class="class"` -- Additional class values for the outer element
* `style="style"` -- CSS style attribute

The data provided is:

* `id` - The ID attribute if any (`undefined` otherwise)
* `style` - The style attribute if any (`undefined` otherwise)
* `additionalClasses` -- Any class value supplied, or `undefined`
* `authors` -- The author objects corresponding to the supplied authorcode(s)

160 changes: 106 additions & 54 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

const util = require('util');
const path = require('path');
const akasha = require('akasharender');
const mahabhuta = akasha.mahabhuta;

Expand All @@ -40,82 +41,133 @@ module.exports = class AuthorsPlugin extends akasha.Plugin {
get config() { return this[_plugin_config]; }
get options() { return this[_plugin_options]; }

findAuthor(author) {
for (let aut of this.options.authors) {
this.isAuthor(aut);
if (aut.code === author) {
return aut;
}
};

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;
}
return undefined;
}

// Type Guard
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)}`);
// 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');
} 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)}`);
}
return true;
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;
};

TODO -- support a bio block
const getTemplate = ($element, _default) => {
return $element.attr("template")
? $element.attr("template")
: _default;
};

TODO -- in template if no url then do not output the <a> tag
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) {
const id = $element.attr('id')
? $element.attr('id')
: undefined;
const _classes = $element.attr('class')
? $element.attr('class')
: undefined;
const style = $element.attr('style')
? $element.attr('style')
: undefined;
const template = $element.attr("template")
? $element.attr("template")
: "authors-byline.html.ejs";
const authors = $element.data('authors')
? $element.data('authors')
: "default";
if (typeof authors === 'string') authors = [ authors ];
if (!Array.isArray(authors)) {
throw new Error(`authors-byline invalid author object ${util.inspect(author)}`);
}

let authorList = [];
for (let aut of authors) {
let found = this.array.options.config.plugin(pluginName)
.findAuthor(aut);
if (!found) {
throw new Error(`authors-byline did not find author ${util.inspect(aut)}`);
}
authorList.push(found);
}


/* TODO for an author thumbnail they are to pass in an href
/* TODO for an author thumbnail they are to pass in an href */

TODO write guide page */
// console.log(`AuthorBylineElement ${util.inspect(metadata)}`);

return akasha.partial(this.array.options.config, template, {
id, style,
additionalClasses: _classes,
authors: authorList
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)
});
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"email": "david@davidherron.com",
"url": "http://davidherron.com"
},
"name": "@akashacms/plugin-authors",
"name": "@akashacms/plugins-authors",
"description": "A plugin for AkashaCMS to support acknowledging authors",
"homepage": "http://akashacms.com/plugins/blog-podcast.html",
"homepage": "http://akashacms.com/plugins/authors.html",
"repository": {
"type": "git",
"url": "git://github.com/akashacms/akashacms-plugin-authors.git"
Expand Down
28 changes: 28 additions & 0 deletions partials/authors-bio-block.html.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<div <%
if (typeof additionalClasses !== 'undefined') {
%>class="author-bio-block <%= additionalClasses %>" <%
} else {
%>class="author-bio-block" <%
}
if (typeof id !== 'undefined' && id) {
%>id="<%= id %>" <%
}
if (typeof style !== 'undefined' && style) {
%>style="<%= style %>" <%
}
%>>
<%
for (let author of authors) {
if (typeof author.bio === 'string') {
%>
<div class="author-bio-block-name">
<%= author.fullname %>
</div>
<div class="author-bio-block-bio">
<%- author.bio %>
</div>
<%
}
}
%>
</div>
10 changes: 5 additions & 5 deletions partials/authors-byline.html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ By <%
if (count++ > 0) {
%>, <%
}
%>
<span itemprop="name" class="author-name-wrapper">
<a href="<%= url %>" rel="author" itemprop="url">
<%= fullname %></a></span>
<%
if (typeof author.url !== 'undefined') {
%><span itemprop="name" class="author-name-wrapper"><a href="<%= author.url %>" rel="author" itemprop="url"><%= author.fullname %></a></span><%
} else {
%><%= author.fullname %><%
}
}
%>
</div>
61 changes: 61 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

const akasha = require('akasharender');
const plugin = require('../index');
const { assert } = require('chai');
const cheerio = require('cheerio');

const authorconfig = {
default: "boygeorge",
authors: [
{
code: "boygeorge",
fullname: "Boy George",
url: "URL"
},
{
code: "eltonjohn",
fullname: "Elton John",
url: "URL"
}
]
};

const config = new akasha.Configuration();
config.rootURL("https://example.akashacms.com");
config.configDir = __dirname;
config.use(require('../index'), authorconfig);
config.setMahabhutaConfig({
recognizeSelfClosing: true,
recognizeCDATA: true,
decodeEntities: true
});
config.prepare();

describe('default author', function() {
it('should find the default author', async function() {
let html = await plugin.process(`
<authors-byline></authors-byline>
`, {}, authorconfig);

assert.exists(html, 'result exists');
assert.isString(html, 'result isString');
assert.include(html, "Boy George");
assert.notInclude(html, "Elton John");
});
});

describe('chosen author', function() {
it('should find the chosen author', async function() {
let html = await plugin.process(`
<authors-byline></authors-byline>
`, {
authors: "eltonjohn"
}, authorconfig);

assert.exists(html, 'result exists');
assert.isString(html, 'result isString');
assert.notInclude(html, "Boy George");
assert.include(html, "Elton John");
});

});
Loading

0 comments on commit d182653

Please sign in to comment.