mirror of
https://github.com/marktext/marktext.git
synced 2025-05-03 05:40:39 +08:00
Respect existing image title if no source is specified (#599)
This commit is contained in:
parent
a77b9ab6f1
commit
638f65e4ea
2
.github/CHANGELOG.md
vendored
2
.github/CHANGELOG.md
vendored
@ -6,6 +6,8 @@
|
||||
|
||||
**:butterfly:Optimization**
|
||||
|
||||
- Respect existing image title if no source is specified (#562)
|
||||
|
||||
**:beetle:Bug fix**
|
||||
|
||||
- Fix dark preview box background color (#587)
|
||||
|
@ -176,6 +176,7 @@ const formatCtrl = ContentState => {
|
||||
|
||||
ContentState.prototype.insertImage = function (url) {
|
||||
const title = /\/?([^./]+)\.[a-z]+$/.exec(url)[1] || ''
|
||||
const encodeUrl = encodeURI(url)
|
||||
const { start, end } = this.cursor
|
||||
const { formats } = this.selectionFormats({ start, end })
|
||||
const { key, offset: startOffset } = start
|
||||
@ -185,29 +186,38 @@ const formatCtrl = ContentState => {
|
||||
const imageFormat = formats.filter(f => f.type === 'image')
|
||||
|
||||
if (imageFormat.length === 1) {
|
||||
// replace pre image
|
||||
// Replace already existing image
|
||||
let imageTitle = title
|
||||
|
||||
// Extract title from image if there isn't an image source already (GH#562). E.g: ![old-title]()
|
||||
if (imageFormat[0].alt && !imageFormat[0].src) {
|
||||
imageTitle = imageFormat[0].alt
|
||||
}
|
||||
|
||||
const { start, end } = imageFormat[0].range
|
||||
block.text = text.substring(0, start) +
|
||||
`` +
|
||||
`` +
|
||||
text.substring(end)
|
||||
|
||||
this.cursor = {
|
||||
start: { key, offset: start + 2 },
|
||||
end: { key, offset: start + 2 + title.length }
|
||||
end: { key, offset: start + 2 + imageTitle.length }
|
||||
}
|
||||
} else if (key !== end.key) {
|
||||
// Replace multi-line text
|
||||
const endBlock = this.getBlock(end.key)
|
||||
const { text } = endBlock
|
||||
endBlock.text = text.substring(0, endOffset) + `` + text.substring(endOffset)
|
||||
endBlock.text = text.substring(0, endOffset) + `` + text.substring(endOffset)
|
||||
const offset = endOffset + 2
|
||||
this.cursor = {
|
||||
start: { key: end.key, offset },
|
||||
end: { key: end.key, offset: offset + title.length }
|
||||
}
|
||||
} else {
|
||||
// Replace single-line text
|
||||
const imageTitle = startOffset !== endOffset ? text.substring(startOffset, endOffset) : title
|
||||
block.text = text.substring(0, start.offset) +
|
||||
`` +
|
||||
`` +
|
||||
text.substring(end.offset)
|
||||
|
||||
this.cursor = {
|
||||
|
Loading…
Reference in New Issue
Block a user