mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 17:56:25 +00:00
Migrate attachments from /images to /attachments -> fix #1941
This commit is contained in:
@@ -391,7 +391,7 @@ export default class MarkdownPreview extends React.Component {
|
|||||||
el.removeEventListener('click', this.linkClickHandler)
|
el.removeEventListener('click', this.linkClickHandler)
|
||||||
})
|
})
|
||||||
|
|
||||||
const { theme, indentSize, showCopyNotification, storagePath } = this.props
|
const { theme, indentSize, showCopyNotification, storagePath, noteKey } = this.props
|
||||||
let { value, codeBlockTheme } = this.props
|
let { value, codeBlockTheme } = this.props
|
||||||
|
|
||||||
this.refs.root.contentWindow.document.body.setAttribute('data-theme', theme)
|
this.refs.root.contentWindow.document.body.setAttribute('data-theme', theme)
|
||||||
@@ -402,7 +402,8 @@ export default class MarkdownPreview extends React.Component {
|
|||||||
value = value.replace(codeBlock, htmlTextHelper.encodeEntities(codeBlock))
|
value = value.replace(codeBlock, htmlTextHelper.encodeEntities(codeBlock))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
const renderedHTML = this.markdown.render(value)
|
let renderedHTML = this.markdown.render(value)
|
||||||
|
attachmentManagement.migrateAttachments(renderedHTML, 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) => {
|
||||||
|
|||||||
@@ -73,6 +73,31 @@ function createAttachmentDestinationFolder (destinationStoragePath, noteKey) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Moves attachments from the old location ('/images') to the new one ('/attachments/noteKey)
|
||||||
|
* @param renderedHTML HTML of the current note
|
||||||
|
* @param storagePath Storage path of the current note
|
||||||
|
* @param noteKey Key of the current note
|
||||||
|
*/
|
||||||
|
function migrateAttachments (renderedHTML, storagePath, noteKey) {
|
||||||
|
if (sander.existsSync(path.join(storagePath, 'images'))) {
|
||||||
|
const attachments = getAttachmentsInContent(renderedHTML) || []
|
||||||
|
if (attachments !== []) {
|
||||||
|
createAttachmentDestinationFolder(storagePath, noteKey)
|
||||||
|
}
|
||||||
|
for (const attachment of attachments) {
|
||||||
|
let attachmentBaseName = path.basename(attachment)
|
||||||
|
const possibleLegacyPath = path.join(storagePath, 'images', attachmentBaseName)
|
||||||
|
if (sander.existsSync(possibleLegacyPath)) {
|
||||||
|
const destinationPath = path.join(storagePath, DESTINATION_FOLDER, attachmentBaseName)
|
||||||
|
if (!sander.existsSync(destinationPath)) {
|
||||||
|
sander.copyFileSync(possibleLegacyPath).to(destinationPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Fixes the URLs embedded in the generated HTML so that they again refer actual local files.
|
* @description Fixes the URLs embedded in the generated HTML so that they again refer actual local files.
|
||||||
* @param {String} renderedHTML HTML in that the links should be fixed
|
* @param {String} renderedHTML HTML in that the links should be fixed
|
||||||
@@ -165,7 +190,7 @@ function handlePastImageEvent (codeEditor, storageKey, noteKey, dataTransferItem
|
|||||||
*/
|
*/
|
||||||
function getAttachmentsInContent (markdownContent) {
|
function getAttachmentsInContent (markdownContent) {
|
||||||
const preparedInput = markdownContent.replace(new RegExp(mdurl.encode(path.sep), 'g'), path.sep)
|
const preparedInput = markdownContent.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')
|
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)
|
return preparedInput.match(regexp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,7 +249,7 @@ function replaceNoteKeyWithNewNoteKey (noteContent, oldNoteKey, newNoteKey) {
|
|||||||
* @returns {String} Input without the references
|
* @returns {String} Input without the references
|
||||||
*/
|
*/
|
||||||
function removeStorageAndNoteReferences (input, noteKey) {
|
function removeStorageAndNoteReferences (input, noteKey) {
|
||||||
return input.replace(new RegExp(mdurl.encode(path.sep), 'g'), path.sep).replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp(path.sep) + noteKey, 'g'), DESTINATION_FOLDER)
|
return input.replace(new RegExp(mdurl.encode(path.sep), 'g'), path.sep).replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + '(' + escapeStringRegexp(path.sep) + noteKey + ')?', 'g'), DESTINATION_FOLDER)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -321,6 +346,7 @@ module.exports = {
|
|||||||
deleteAttachmentsNotPresentInNote,
|
deleteAttachmentsNotPresentInNote,
|
||||||
moveAttachments,
|
moveAttachments,
|
||||||
cloneAttachments,
|
cloneAttachments,
|
||||||
|
migrateAttachments,
|
||||||
STORAGE_FOLDER_PLACEHOLDER,
|
STORAGE_FOLDER_PLACEHOLDER,
|
||||||
DESTINATION_FOLDER
|
DESTINATION_FOLDER
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user