Skip to content

Commit

Permalink
Merge pull request #103 from curbengh/misc-fix
Browse files Browse the repository at this point in the history
fix(import-image): regex and image slug
  • Loading branch information
curbengh committed Sep 1, 2020
2 parents 0bc207b + 88a3448 commit b87afcf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/migrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = async function(args) {
const rExcerpt = /<!-- ?more ?-->/i;
const postExcerpt = '\n<!-- more -->\n';
const posts = [];
const rImg = /!\[.*\]\((.*)\)/g;
const rImg = /!\[.*\]\((.+\.(jp(e)?g|png|gif|webp))\)/g;
const images = {};
const rEntity = /&#?\w{2,4};/;
let currentPosts = [];
Expand Down Expand Up @@ -123,8 +123,8 @@ module.exports = async function(args) {
if (!post_asset_folder) {
await writeFile(join(source_dir, imagePath), data);
} else {
const rTitle = new RegExp(title + '/?$');
post_slug = basename(link.replace(rTitle, ''));
const rSlug = new RegExp(slug + '/?$');
post_slug = basename(link.replace(rSlug, ''));
await writeFile(join(source_dir, '_posts', post_slug, imagePath), data);
}

Expand Down
24 changes: 22 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,15 @@ describe('migrator', function() {

describe('import image', () => {
const postTitle = 'foo';
const imgTitle = 'image';
const imgTitle = 'A Big Image';
const imgSlug = 'image';
const wp = (imageUrl = '', imagePath = '', content = '') => {
return `<rss><channel><title>test</title>
<item><title>${postTitle}</title><wp:post_type>post</wp:post_type>
<content:encoded><![CDATA[${content}]]></content:encoded></item>
<item><title>${imgTitle}</title>
<link>http://localhost/wp/2020/07/07/${postTitle}/${imgTitle}/</link>
<link>http://localhost/wp/2020/07/07/${postTitle}/${imgSlug}/</link>
<wp:post_name>${imgSlug}</wp:post_name>
<wp:post_type>attachment</wp:post_type>
<wp:attachment_url>${imageUrl}</wp:attachment_url>
<wp:postmeta>
Expand Down Expand Up @@ -497,6 +499,24 @@ describe('migrator', function() {
await unlink(path);
});

// #102
it('content with ()', async () => {
const imageUrl = 'https://raw.githubusercontent.com/hexojs/hexo-migrator-wordpress/master/test/fixtures/hexo.png';
const imagePath = '2020/07/hexo.png';
const imgEmbed = `<p><img src="${imageUrl}" alt="${imageUrl}" />Lorem ipsum dolor sit amet (consectetur adipiscing elit)</p>`;
const xml = wp(imageUrl, imagePath, imgEmbed);
const path = join(__dirname, 'image.xml');
await writeFile(path, xml);
await m({ _: [path], 'import-image': true });

const rendered = await readFile(join(hexo.source_dir, '_posts', postTitle + '.md'));
const output = parsePost(rendered, true);

output.should.eql(md(imgEmbed).replace(imageUrl + ')', '/' + imagePath + ')'));

await unlink(path);
});

it('resized image', async () => {
const imageUrl = 'https://raw.githubusercontent.com/hexojs/hexo-migrator-wordpress/master/test/fixtures/hexo.jpg';
const imagePath = '2020/07/hexo.jpg';
Expand Down

0 comments on commit b87afcf

Please sign in to comment.