Skip to content

Commit

Permalink
fix: extract excerpt before markdown conversion (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Jul 4, 2020
1 parent 4df53be commit 68ea753
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/migrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ module.exports = async function(args) {
let errNum = 0;
let skipNum = 0;
let input, feed;
const rExcerpt = /<a id="more"><\/a>/i;
const rExcerpt = /<!--+\s*more\s*--+>/i;
const postExcerpt = '\n<!-- more -->\n';
const posts = [];
let currentPosts = [];

const md = str => {
return tomd.turndown(str);
};

try {
if (!source) {
const help = [
Expand Down Expand Up @@ -59,19 +63,27 @@ module.exports = async function(args) {
let { title, content, description } = item;

const layout = status === 'draft' ? 'draft' : 'post';
content = tomd.turndown(content).replace(/\r\n/g, '\n');

if (type !== 'page') {
// Apply 'limit' option to post only
if (postLimit >= limit) continue;
postLimit++;

if (rExcerpt.test(content)) {
content.replace(rExcerpt, postExcerpt);
content.replace(rExcerpt, (match, index) => {
const excerpt = md(content.substring(0, index).trim());
const more = md(content.substring(index + match.length).trim());

content = excerpt + postExcerpt + more;
});
} else if (description) {
description = tomd.turndown(description).replace(/\r\n/g, '\n');
description = md(description);
content = description + postExcerpt + content;
} else {
content = md(content);
}
} else {
content = md(content);
}

if (!title) {
Expand Down
36 changes: 36 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,42 @@ describe('migrator', function() {
posts.includes(unescape(percentEncoded.slug) + '.md').should.eql(true);
});

it('excerpt', async () => {
const content = 'foo<!-- more -->bar';
const xml = `<rss><channel><title>test</title>
<item><title>baz</title><content:encoded><![CDATA[${content}]]></content:encoded></item>
</channel></rss>`;
const path = join(__dirname, 'excerpt.xml');
await writeFile(path, xml);
await m({ _: [path] });

const rendered = await readFile(join(hexo.source_dir, '_posts', 'baz.md'));
const rFrontMatter = /^([\s\S]+?)\n(-{3,}|;{3,})(?:$|\n([\s\S]*)$)/;
const output = rendered.match(rFrontMatter)[3].replace(/\r?\n|\r/g, '');

output.should.eql(content);

await unlink(path);
});

it('excerpt - wp:more', async () => {
const content = 'foo<!-- wp:more --><!-- more --><!-- wp:more -->bar';
const xml = `<rss><channel><title>test</title>
<item><title>baz</title><content:encoded><![CDATA[${content}]]></content:encoded></item>
</channel></rss>`;
const path = join(__dirname, 'excerpt.xml');
await writeFile(path, xml);
await m({ _: [path] });

const rendered = await readFile(join(hexo.source_dir, '_posts', 'baz.md'));
const rFrontMatter = /^([\s\S]+?)\n(-{3,}|;{3,})(?:$|\n([\s\S]*)$)/;
const output = rendered.match(rFrontMatter)[3].replace(/\r?\n|\r/g, '');

output.should.eql(content.replace(/<!-- wp:more -->/g, ''));

await unlink(path);
});

it('no argument', async () => {
try {
await m({ _: [''] });
Expand Down

0 comments on commit 68ea753

Please sign in to comment.