The proper way to handle this now is using getFrontmatterInfo
(obsidian-api/obsidian.d.ts at 8b2eda0f24285636c8aa116972643e5233a23dc1 · obsidianmd/obsidian-api · GitHub)
let { contentStart } = getFrontMatterInfo(fileContents);
// removing the frontmatter from a file
let withoutFrontmatter = fileContents.slice(contentStart);
// swapping the frontmatter
let replacedFrontmatter = '---\nfoo: bar\n---\n' + fileContents.slice(contentStart);
// prepending text to file without breaking frontmatter
let withPrependedText = fileContents.slice(0, contentStart) + 'hello' + fileContents.slice(contentStart);