mirror of
https://github.com/marktext/marktext.git
synced 2025-05-03 19:30:19 +08:00
Added Regex group replace on SearchMenu (#2205)
Co-authored-by: albert <albert.giro@softtek.com>
This commit is contained in:
parent
c7060934ce
commit
ead35e1819
@ -34,11 +34,23 @@ const matchString = (text, value, options) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const searchCtrl = ContentState => {
|
const searchCtrl = ContentState => {
|
||||||
|
ContentState.prototype.buildRegexValue = function (match, value) {
|
||||||
|
const groups = value.match(/\$(\d)*/g)
|
||||||
|
if (groups) {
|
||||||
|
for (const regexGroup of groups) {
|
||||||
|
const groupIndex = regexGroup[1] - 1
|
||||||
|
if (groupIndex < match.subMatches.length) {
|
||||||
|
value = value.replace(regexGroup, match.subMatches[groupIndex])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
ContentState.prototype.replaceOne = function (match, value) {
|
ContentState.prototype.replaceOne = function (match, value) {
|
||||||
const { start, end, key } = match
|
const { start, end, key } = match
|
||||||
const block = this.getBlock(key)
|
const block = this.getBlock(key)
|
||||||
const { text } = block
|
const { text } = block
|
||||||
|
|
||||||
block.text = text.substring(0, start) + value + text.substring(end)
|
block.text = text.substring(0, start) + value + text.substring(end)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,6 +60,9 @@ const searchCtrl = ContentState => {
|
|||||||
const searchOptions = Object.assign({}, defaultSearchOption, opt)
|
const searchOptions = Object.assign({}, defaultSearchOption, opt)
|
||||||
const { matches, value, index } = this.searchMatches
|
const { matches, value, index } = this.searchMatches
|
||||||
if (matches.length) {
|
if (matches.length) {
|
||||||
|
if (opt.isRegexp) {
|
||||||
|
replaceValue = this.buildRegexValue(matches[index], replaceValue)
|
||||||
|
}
|
||||||
if (isSingle) {
|
if (isSingle) {
|
||||||
this.replaceOne(matches[index], replaceValue)
|
this.replaceOne(matches[index], replaceValue)
|
||||||
} else {
|
} else {
|
||||||
@ -108,7 +123,8 @@ const searchCtrl = ContentState => {
|
|||||||
return {
|
return {
|
||||||
key,
|
key,
|
||||||
start: m.index,
|
start: m.index,
|
||||||
end: m.index + m.match.length
|
end: m.index + m.match.length,
|
||||||
|
subMatches: m.subMatches
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user