1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 17:56:25 +00:00

Use markdown content for migrateAttachments

This commit is contained in:
Santiago Agüero
2018-07-17 00:13:13 -03:00
parent 5cc52f91cb
commit c37b780ca4
3 changed files with 22 additions and 40 deletions

View File

@@ -450,7 +450,7 @@ export default class MarkdownPreview extends React.Component {
}) })
} }
const renderedHTML = this.markdown.render(value) const renderedHTML = this.markdown.render(value)
attachmentManagement.migrateAttachments(renderedHTML, storagePath, noteKey) attachmentManagement.migrateAttachments(value, storagePath, noteKey)
this.refs.root.contentWindow.document.body.innerHTML = attachmentManagement.fixLocalURLS(renderedHTML, storagePath) this.refs.root.contentWindow.document.body.innerHTML = attachmentManagement.fixLocalURLS(renderedHTML, storagePath)
_.forEach(this.refs.root.contentWindow.document.querySelectorAll('input[type="checkbox"]'), (el) => { _.forEach(this.refs.root.contentWindow.document.querySelectorAll('input[type="checkbox"]'), (el) => {

View File

@@ -75,17 +75,15 @@ function createAttachmentDestinationFolder (destinationStoragePath, noteKey) {
} }
} }
// TODO: This function does not have unit test. Is it working?
// TODO: Can we rewrite it to use markdownContent instead of renderedHTML? That way we can keep one function
/** /**
* @description Moves attachments from the old location ('/images') to the new one ('/attachments/noteKey) * @description Moves attachments from the old location ('/images') to the new one ('/attachments/noteKey)
* @param renderedHTML HTML of the current note * @param markdownContent of the current note
* @param storagePath Storage path of the current note * @param storagePath Storage path of the current note
* @param noteKey Key of the current note * @param noteKey Key of the current note
*/ */
function migrateAttachments (renderedHTML, storagePath, noteKey) { function migrateAttachments (markdownContent, storagePath, noteKey) {
if (sander.existsSync(path.join(storagePath, 'images'))) { if (sander.existsSync(path.join(storagePath, 'images'))) {
const attachments = getAttachmentsInContent(renderedHTML) || [] const attachments = getAttachmentsInMarkdownContent(markdownContent) || []
if (attachments !== []) { if (attachments !== []) {
createAttachmentDestinationFolder(storagePath, noteKey) createAttachmentDestinationFolder(storagePath, noteKey)
} }
@@ -202,17 +200,6 @@ function getAttachmentsInMarkdownContent (markdownContent) {
return preparedInput.match(regexp) return preparedInput.match(regexp)
} }
/**
* @description Returns all attachment paths of the given renderedHTML
* @param {String} renderedHTML content in which the attachment paths should be found
* @returns {String[]} Array of the relative paths (starting with :storage) of the attachments of the given markdown
*/
function getAttachmentsInContent (renderedHTML) {
const preparedInput = renderedHTML.replace(new RegExp(mdurl.encode(path.sep), 'g'), path.sep)
const regexp = new RegExp('/?' + STORAGE_FOLDER_PLACEHOLDER + '(' + escapeStringRegexp(path.sep) + '|/)' + '?([a-zA-Z0-9]|-)*' + '(' + escapeStringRegexp(path.sep) + '|/)' + '([a-zA-Z0-9]|\\.)+(\\.[a-zA-Z0-9]+)?', 'g')
return preparedInput.match(regexp)
}
/** /**
* @description Returns an array of the absolute paths of the attachments referenced in the given markdown code * @description Returns an array of the absolute paths of the attachments referenced in the given markdown code
* @param {String} markdownContent content in which the attachment paths should be found * @param {String} markdownContent content in which the attachment paths should be found
@@ -426,7 +413,6 @@ module.exports = {
generateAttachmentMarkdown, generateAttachmentMarkdown,
handleAttachmentDrop, handleAttachmentDrop,
handlePastImageEvent, handlePastImageEvent,
getAttachmentsInContent,
getAttachmentsInMarkdownContent, getAttachmentsInMarkdownContent,
getAbsolutePathsOfAttachmentsInContent, getAbsolutePathsOfAttachmentsInContent,
removeStorageAndNoteReferences, removeStorageAndNoteReferences,

View File

@@ -217,28 +217,24 @@ it('should test that generateAttachmentMarkdown works correct both with previews
expect(actual).toEqual(expected) expect(actual).toEqual(expected)
}) })
it('should test that getAttachmentsInContent finds all attachments', function () { it('should test that migrateAttachments work when they have different path separators', function () {
const testInput = sander.existsSync = jest.fn(() => true)
'<html>\n' + const dummyStoragePath = 'dummyStoragePath'
' <head>\n' + const imagesPath = path.join(dummyStoragePath, 'images')
' //header\n' + const attachmentsPath = path.join(dummyStoragePath, 'attachments')
' </head>\n' + const noteKey = 'noteKey'
' <body data-theme="default">\n' + const testInput = '"# Test\n' +
' <h2 data-line="0" id="Headline">Headline</h2>\n' + '\n' +
' <p data-line="2">\n' + '![Screenshot1](:storage' + path.win32.sep + '0.3b88d0dc.png)\n' +
' <img src=":storage' + mdurl.encode(path.sep) + '9c9c4ba3-bc1e-441f-9866-c1e9a806e31c' + mdurl.encode(path.sep) + '0.6r4zdgc22xp.png" alt="dummyImage.png" >\n' + '![Screenshot2](:storage' + path.posix.sep + '0.2cb8875c.pdf)"'
' </p>\n' +
' <p data-line="4">\n' + systemUnderTest.migrateAttachments(testInput, dummyStoragePath, noteKey)
' <a href=":storage' + mdurl.encode(path.sep) + '9c9c4ba3-bc1e-441f-9866-c1e9a806e31c' + mdurl.encode(path.sep) + '0.q2i4iw0fyx.pdf">dummyPDF.pdf</a>\n' +
' </p>\n' + expect(sander.existsSync.mock.calls[0][0]).toBe(imagesPath)
' <p data-line="6">\n' + expect(sander.existsSync.mock.calls[1][0]).toBe(path.join(imagesPath, '0.3b88d0dc.png'))
' <img src=":storage' + mdurl.encode(path.sep) + '9c9c4ba3-bc1e-441f-9866-c1e9a806e31c' + mdurl.encode(path.sep) + 'd6c5ee92.jpg" alt="dummyImage2.jpg">\n' + expect(sander.existsSync.mock.calls[2][0]).toBe(path.join(attachmentsPath, '0.3b88d0dc.png'))
' </p>\n' + expect(sander.existsSync.mock.calls[3][0]).toBe(path.join(imagesPath, '0.2cb8875c.pdf'))
' </body>\n' + expect(sander.existsSync.mock.calls[4][0]).toBe(path.join(attachmentsPath, '0.2cb8875c.pdf'))
'</html>'
const actual = systemUnderTest.getAttachmentsInContent(testInput)
const expected = [':storage' + path.sep + '9c9c4ba3-bc1e-441f-9866-c1e9a806e31c' + path.sep + '0.6r4zdgc22xp.png', ':storage' + path.sep + '9c9c4ba3-bc1e-441f-9866-c1e9a806e31c' + path.sep + '0.q2i4iw0fyx.pdf', ':storage' + path.sep + '9c9c4ba3-bc1e-441f-9866-c1e9a806e31c' + path.sep + 'd6c5ee92.jpg']
expect(actual).toEqual(expect.arrayContaining(expected))
}) })
it('should test that getAttachmentsInMarkdownContent finds all attachments when they have different path separators', function () { it('should test that getAttachmentsInMarkdownContent finds all attachments when they have different path separators', function () {