* Rewrite inline field values in raw note content using the same readable-line * boundary as parseInlineContent. Only the changed line spans are * replaced; all untouched bytes, including mixed CRLF/LF line endings, are * preserved exactly.
(content: string, replacements: ReadonlyArray<InlineFieldReplacement>)
| 462 | * preserved exactly. |
| 463 | */ |
| 464 | public replaceInlineFieldValuesInContent(content: string, replacements: ReadonlyArray<InlineFieldReplacement>): string { |
| 465 | const activeReplacements = replacements.filter(({key}) => key.length > 0); |
| 466 | if (activeReplacements.length === 0) return content; |
| 467 | |
| 468 | let result = ""; |
| 469 | let cursor = 0; |
| 470 | |
| 471 | for (const line of this.walkInlineContentLines(content)) { |
| 472 | if (!line.readable) continue; |
| 473 | if (!line.text.includes("::")) continue; |
| 474 | |
| 475 | let updatedLine = line.text; |
| 476 | for (const {key, value} of activeReplacements) { |
| 477 | updatedLine = this.replaceInlineFieldValue(updatedLine, key, value); |
| 478 | } |
| 479 | |
| 480 | if (updatedLine === line.text) continue; |
| 481 | |
| 482 | result += content.slice(cursor, line.start) + updatedLine; |
| 483 | cursor = line.end; |
| 484 | } |
| 485 | |
| 486 | if (cursor === 0) return content; |
| 487 | |
| 488 | return result + content.slice(cursor); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Compute the line index at which to splice a new `name:: value` inline field. |
no test coverage detected