1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/CliClient/tests/MdToMd.js
Bart d9c15b84d0 Desktop: when importing MD files create resources for local linked files (#2262)
* md importer: first pass import attachment resources with markdown files

* md importer: import resources from md - no unneeded saves, check if files exist, regex name

* md importer: test import of local files as resources, separate method for importing linked files, comment regex matching md tags

* md importer: move stateful regex to method scope, remove spurius await

* md importer: lint

* md importer: respond to PR comments: remove test nesting, test sample, check if path is dir, use shim.fsDriver

* md importer: use file-path methods for getting attachment path

* md importer: use extractImageUrls helper, test for file with zero links

* md importer: try catch around importLocalImages, improve test

* md importer: importing attached images cover case where link also appears elsewhere in doc

* md importer: only create 1 resource if note contains duplicate links, test

* md importer: remove log

* md importer: remove use of lodash
2020-01-19 15:39:38 +00:00

42 lines
2.0 KiB
JavaScript

const mdImporterService = require('lib/services/InteropService_Importer_Md');
const Note = require('lib/models/Note.js');
const { setupDatabaseAndSynchronizer, switchClient } = require('test-utils.js');
const importer = new mdImporterService();
describe('InteropService_Importer_Md: importLocalImages', function() {
beforeEach(async (done) => {
await setupDatabaseAndSynchronizer(1);
await switchClient(1);
done();
});
it('should import linked files and modify tags appropriately', async function() {
const tagNonExistentFile = '![does not exist](does_not_exist.png)';
const note = await importer.importFile(`${__dirname}/md_to_md/sample.md`, 'notebook');
let items = await Note.linkedItems(note.body);
expect(items.length).toBe(2);
const inexistentLinkUnchanged = note.body.includes(tagNonExistentFile);
expect(inexistentLinkUnchanged).toBe(true);
});
it('should only create 1 resource for duplicate links, all tags should be updated', async function() {
const note = await importer.importFile(`${__dirname}/md_to_md/sample-duplicate-links.md`, 'notebook');
let items = await Note.linkedItems(note.body);
expect(items.length).toBe(1);
const reg = new RegExp(items[0].id, 'g');
const matched = note.body.match(reg);
expect(matched.length).toBe(2);
});
it('should import linked files and modify tags appropriately when link is also in alt text', async function() {
const note = await importer.importFile(`${__dirname}/md_to_md/sample-link-in-alt-text.md`, 'notebook');
let items = await Note.linkedItems(note.body);
expect(items.length).toBe(1);
});
it('should passthrough unchanged if no links present', async function() {
const note = await importer.importFile(`${__dirname}/md_to_md/sample-no-links.md`, 'notebook');
let items = await Note.linkedItems(note.body);
expect(items.length).toBe(0);
expect(note.body).toContain('Unidentified vessel travelling at sub warp speed, bearing 235.7. Fluctuations in energy readings from it, Captain. All transporters off.');
});
});