-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathblockquote.js
69 lines (56 loc) · 1.68 KB
/
blockquote.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
'use strict';
// Based on: https://raw.github.com/imathis/octopress/master/plugins/blockquote.rb
const titlecase = require('titlecase');
const rFullCiteWithTitle = /(\S.*)\s+(https?:\/\/\S+)\s+(.+)/i;
const rFullCite = /(\S.*)\s+(https?:\/\/\S+)/i;
const rAuthorTitle = /([^,]+),\s*([^,]+)/;
/**
* @param {string[]} args
* @param {Hexo} ctx
*/
const parseFooter = (args, ctx) => {
const str = args.join(' ');
if (!str) return '';
let author = '';
let source = '';
let title = '';
let match;
if ((match = rFullCiteWithTitle.exec(str))) {
author = match[1];
source = match[2];
title = ctx.config.titlecase ? titlecase(match[3]) : match[3];
} else if ((match = rFullCite.exec(str))) {
author = match[1];
source = match[2];
} else if ((match = rAuthorTitle.exec(str))) {
author = match[1];
title = ctx.config.titlecase ? titlecase(match[2]) : match[2];
} else {
author = str;
}
let footer = '';
if (author) footer += `<strong>${author}</strong>`;
if (source) {
const link = source.replace(/^https?:\/\/|\/(index.html?)?$/g, '');
footer += `<cite><a href="${source}">${title ? title : link}</a></cite>`;
} else if (title) {
footer += `<cite>${title}</cite>`;
}
return footer;
};
/**
* Blockquote tag
*
* Syntax:
* {% blockquote [author[, source]] [link] [source_link_title] %}
* Quote string
* {% endblockquote %}
*/
module.exports = ctx => function blockquoteTag(args, content) {
const footer = parseFooter(args, ctx);
let result = '<blockquote>';
result += ctx.render.renderSync({text: content, engine: 'markdown'});
if (footer) result += `<footer>${footer}</footer>`;
result += '</blockquote>';
return result;
};