Hi, I’m trying to develop a plugin, which needs to access frontmatter to read user per-file default configurations.
I found that MarkdownPostProcessorContext
has the frontmatter
member, but when I access it, it is empty.
I also found I can access the last frontmatter from app.workspace.activeEditor.lastFrontmatter
, which is an undocumented member.
I wonder for the first case, how to correctly get the frontmatter, and for the second case, is it a safe and backward-compatible implementation?
Thanks!
drongo
April 17, 2023, 2:39pm
2
I’m using gray-matter . I’ve not found a way to reliably get the current (rather than a few seconds old) frontmatter, without external dependencies.
MarkdownPostProcessorContext.getSectionInfo() gives you the first and last line numbers of the code block.
MarkdownView.editor gives you an Editor instance for the file in that View. (WorkspaceLeaf.view.editor)
drongo
April 17, 2023, 2:54pm
3
import GrayMatter from "gray-matter"
const EditorLine = (line: number): EditorPosition => ({ line, ch: 0 })
function codeBlockHandler(markdown: string, containerEl: HTMLDivElement, ctx: MarkdownPostProcessorContext) {
const leaf: WorkspaceLeaf = app.workspace.rootSplit.children[0].children[0]
const view: MarkdownView = leaf.view
const editor: Editor = view.editor
const sectionInfo: MarkdownSectionInformation = ctx.getSectionInfo()
const lineStart = EditorLine(sectionInfo.lineStart + 1)
const lineEnd = EditorLine(sectionInfo.lineEnd )
const text = editor.getRange(lineStart, lineEnd)
const gm = GrayMatter(text)
const frontmatter = gm.data
}
drongo
April 17, 2023, 3:06pm
4
Finding the associated WorkspaceLeaf is another non-trivial problem.
You need to iterate through all leaves to find one where leaf.containerEl.contains(codeBlock.containerEl)
. When the leaf is first created, the codeBlock handler is executed first, so you have to wait until after this function has finished executing and the leaf has been constructed, before you can find a leaf that matches that condition.
Got it, thanks for your quick reply.
drongo
April 18, 2023, 11:13am
6
And if you want to update the frontmatter:
update(gm.data)
editor.replaceRange(
GrayMatter.stringify(gm.content, gm.data),
lineStart, lineEnd
)