Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spread function: Hexo built-in plugin blockquote #3291

Merged
merged 7 commits into from
Nov 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 44 additions & 40 deletions lib/plugins/tag/blockquote.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,63 @@

const titlecase = require('titlecase');

const rFullCiteWithTitle = /(\S.*)\s+(https?:\/\/)(\S+)\s+(.+)/i;
const rFullCite = /(\S.*)\s+(https?:\/\/)(\S+)/i;
const rFullCiteWithTitle = /(\S.*)\s+(https?:\/\/\S+)\s+(.+)/i;
const rFullCite = /(\S.*)\s+(https?:\/\/\S+)/i;
const rAuthorTitle = /([^,]+),\s*([^,]+)/;
const rAuthor = /(.+)/;

/**
* Blockquote tag
*
* Syntax:
* {% blockquote [author[, source]] [link] [source_link_title] %}
* Quote string
* {% endblockquote %}
*/

module.exports = ctx => function blockquoteTag(args, content) {
* @param {string[]} args
* @param {Hexo} ctx
*/
const parseFooter = (args, ctx) => {
const str = args.join(' ');
if (!str) return '';

let author = '';
let source = '';
let title = '';
let footer = '';
let result = '';
let match;

if (str) {
if (rFullCiteWithTitle.test(str)) {
match = str.match(rFullCiteWithTitle);
author = match[1];
source = match[2] + match[3];
title = ctx.config.titlecase ? titlecase(match[4]) : match[4];
} else if (rFullCite.test(str)) {
match = str.match(rFullCite);
author = match[1];
source = match[2] + match[3];
} else if (rAuthorTitle.test(str)) {
match = str.match(rAuthorTitle);
author = match[1];
title = ctx.config.titlecase ? titlecase(match[2]) : match[2];
} else if (rAuthor.test(str)) {
match = str.match(rAuthor);
author = match[1];
}
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;
}

if (author) footer += `<strong>${author}</strong>`;
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>`;
}
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>`;
}

result += '<blockquote>';
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>';
Expand Down