How do i edit the contents of my active file (in the code-block)

Hi, I am new to developing obsidian plugin and do not understand the Obsidian’s API completely. I apologise if this comes across as a stupid question or a bother but i thought this would be the fastest way to find an answer as i could not find it on google.

I have a plugin where i am taking YAML syntax input in the code-block and then telling how to display it in markdown using “registerMarkdownCodeBlockProcessor” function. I wish to also add a button to the rendered view that would change a value that i was initially taking in from the code-block. Now i could just change how it is displaying in the code itself but it would not stay there after a reload, for that i also have to update it in the code-block. Is there a way to do it? if there is then can u direct me on what API functions it would require?

Also if someone is willing to go the extra mile for lending a hand, could you please share some good documentation which would preferably include explanations and examples of the obsidian API if such exists. Or just a general suggestion on where to get started from properly? I have a few bits that i wanted to make.

Not quite sure, but the way I would go about this would probably be to find the code block using some kind of regex match along the lines of /```my-code-block\n((.|\n)+?)```/gm - be sure to change “my-code-block” to the type of code block you want to match, and the first match group should be the content of the block. You can try it here.

To get the file content for this, you can use app.vault.read(ctx.sourcePath), where ctx is the MarkdownPostProcessorContext parameter.

Then edit the content using app.vault.modify(...) and app.vault.getFileByPath(ctx.sourcePath) to get the file object.

By the way, here are the API docs: https://docs.obsidian.md/Home :slight_smile:

Being a newbie myself, there may well be a far better way to do this, so I am open to suggestions!

I know you’re working in a plugin context, but here is a dataviewjs script to list out the first 15 code sections of your vault:

```dataviewjs

const result = []

for (let [fname, fcache] of Object.entries(dv.app.metadataCache.fileCache)) {
  // fname is the filename
  // fcache is the entry from metadataCache.fileCache
  // mcache is the actual metadataCache.metadataCache entry
  const mcache = dv.app.metadataCache.metadataCache[fcache.hash]
  
  if ( mcache && !mcache.frontmatter &&
       mcache.hasOwnProperty("sections") &&
       mcache["sections"].some(s => s.type == "code")
     ) {
     
     for (const section of mcache["sections"]) {
       if (section.type == "code")
          result.push([fname, section.position.start.line, section.position.end.line])
     }
     // console.log(mcache.sections)
  }   
  //console.log(fname, " » ", fcache, "\n  »» ", mcache)
  
  // Bail out after the first 15 entries
  if (result.length > 15) break
}

dv.table(["Filename", "Code start line", "Code end line"], result)
```

The same kind of logic should be possible to use within a plugin to:

From there you should be able to have proper references to where in the original file(s) the code sections are, and you can use some of the editor functions in combination with app.vault.process() to change the files themselves.

1 Like