Skip to content

Commit e864645

Browse files
authored
Merge pull request #730 from asmsuechan/make-image-path-relative
Make image path relative
2 parents 0266770 + e8553ca commit e864645

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

browser/components/CodeEditor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ export default class CodeEditor extends React.Component {
192192
const imagePath = e.dataTransfer.files[0].path
193193
const filename = path.basename(imagePath)
194194

195-
copyImage(imagePath, this.props.storageKey).then((imagePathInTheStorage) => {
196-
const imageMd = `![${filename}](${imagePathInTheStorage})`
195+
copyImage(imagePath, this.props.storageKey).then((imagePath) => {
196+
const imageMd = `![${filename}](${path.join('/:storage', imagePath)})`
197197
this.insertImageMd(imageMd)
198198
})
199199
}

browser/components/MarkdownEditor.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import styles from './MarkdownEditor.styl'
44
import CodeEditor from 'browser/components/CodeEditor'
55
import MarkdownPreview from 'browser/components/MarkdownPreview'
66
import eventEmitter from 'browser/main/lib/eventEmitter'
7+
const _ = require('lodash')
78

89
class MarkdownEditor extends React.Component {
910
constructor (props) {
@@ -213,6 +214,11 @@ class MarkdownEditor extends React.Component {
213214
let previewStyle = {}
214215
if (this.props.ignorePreviewPointerEvents) previewStyle.pointerEvents = 'none'
215216

217+
const cachedStorageList = JSON.parse(localStorage.getItem('storages'))
218+
if (!_.isArray(cachedStorageList)) throw new Error('Target storage doesn\'t exist.')
219+
const storage = _.find(cachedStorageList, {key: storageKey})
220+
if (storage === undefined) throw new Error('Target storage doesn\'t exist.')
221+
216222
return (
217223
<div className={className == null
218224
? 'MarkdownEditor'
@@ -260,6 +266,7 @@ class MarkdownEditor extends React.Component {
260266
onMouseUp={(e) => this.handlePreviewMouseUp(e)}
261267
onMouseDown={(e) => this.handlePreviewMouseDown(e)}
262268
onCheckboxClick={(e) => this.handleCheckboxClick(e)}
269+
storagePath={storage.path}
263270
/>
264271
</div>
265272
)

browser/components/MarkdownPreview.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ export default class MarkdownPreview extends React.Component {
255255
el.removeEventListener('click', this.linkClickHandler)
256256
})
257257

258-
let { value, theme, indentSize, codeBlockTheme } = this.props
258+
let { value, theme, indentSize, codeBlockTheme, storagePath } = this.props
259259

260260
this.refs.root.contentWindow.document.body.setAttribute('data-theme', theme)
261261

@@ -283,6 +283,11 @@ export default class MarkdownPreview extends React.Component {
283283
el.addEventListener('click', this.linkClickHandler)
284284
})
285285

286+
_.forEach(this.refs.root.contentWindow.document.querySelectorAll('img'), (el) => {
287+
if (!/\/:storage/.test(el.src)) return
288+
el.src = el.src.replace('/:storage', path.join(storagePath, 'images'))
289+
})
290+
286291
codeBlockTheme = consts.THEMES.some((_theme) => _theme === codeBlockTheme)
287292
? codeBlockTheme
288293
: 'default'
@@ -412,5 +417,6 @@ MarkdownPreview.propTypes = {
412417
onMouseUp: PropTypes.func,
413418
onMouseDown: PropTypes.func,
414419
className: PropTypes.string,
415-
value: PropTypes.string
420+
value: PropTypes.string,
421+
storagePath: PropTypes.string
416422
}

browser/finder/NoteDetail.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ class NoteDetail extends React.Component {
106106
let editorIndentSize = parseInt(config.editor.indentSize, 10)
107107
if (!(editorFontSize > 0 && editorFontSize < 132)) editorIndentSize = 4
108108

109+
const cachedStorageList = JSON.parse(localStorage.getItem('storages'))
110+
if (!_.isArray(cachedStorageList)) throw new Error('Target storage doesn\'t exist.')
111+
const storage = _.find(cachedStorageList, {key: note.storage})
112+
if (storage === undefined) throw new Error('Target storage doesn\'t exist.')
113+
109114
if (note.type === 'SNIPPET_NOTE') {
110115
let tabList = note.snippets.map((snippet, index) => {
111116
let isActive = this.state.snippetIndex === index
@@ -192,6 +197,7 @@ class NoteDetail extends React.Component {
192197
lineNumber={config.preview.lineNumber}
193198
indentSize={editorIndentSize}
194199
value={note.content}
200+
storagePath={storage.path}
195201
/>
196202
)
197203
}

browser/main/lib/dataApi/copyImage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function copyImage (filePath, storageKey) {
2626
if (!fs.existsSync(imageDir)) fs.mkdirSync(imageDir)
2727
const outputImage = fs.createWriteStream(path.join(imageDir, basename))
2828
inputImage.pipe(outputImage)
29-
resolve(`${targetStorage.path}/images/${basename}`)
29+
resolve(basename)
3030
} catch (e) {
3131
return reject(e)
3232
}

0 commit comments

Comments
 (0)