MarkdownCodeBlockProcessor adds extra newline when in reading mode

Steps to reproduce

  • Create a plugin
    • that registers a MarkdownCodeBlockProcessor,
    • which prints the first argument, the block’s content, to console;
    • enable plugin
  • Create a new note
    • write a new markdown code block for the language specified
    ```lang
    foo
    ```
    
    • step out of the code block editor, such that the processor is called
  • Switch from editing view to reading view

Did you follow the troubleshooting guide? [Y/N]

Y

Expected result

I expect the processor to behave the same in editing and reading mode. Thus the expected result in the developer console should be:

  • “foo”
  • “foo”

Actual result

The actual result differs in that an extra new line is passed while the processor is called in reading mode. Developer console:

  • “foo”
  • “foo\n”

Environment

SYSTEM INFO:
Obsidian version: v1.7.4
Installer version: v1.4.13
Operating system: #134~20.04.1-Ubuntu SMP Tue Oct 1 15:27:33 UTC 2024 5.15.0-124-generic
Login status: not logged in
Language: en
Insider build toggle: off
Live preview: on
Base theme: dark
Community theme: none
Snippets enabled: 1
Restricted mode: off
Plugins installed: 10
Plugins enabled: 1
1: Obsidian TikZ v0.1.0

RECOMMENDATIONS:
Community plugins: for bugs, please first try updating all your plugins to latest. If still not fixed, please try to make the issue happen in the Sandbox Vault or disable community plugins.


Additional information

I uploaded a video to Discord

https://discord.com/channels/686053708261228577/840286264964022302/1297294486010990603

Thank you

I see that just printing to console does not show the new line. Here an example that makes it clear.

Plugin

export default class ExamplePlugin extends Plugin {
    async onload() {
        this.registerMarkdownCodeBlockProcessor('lang', (src) => 
            console.log('', { src })
        );
    }
}

Node

```lang
foo
```

Action

  • Write node
  • Leave block, so it processes
  • Switch to reading mode

Expected result

Developer console:

{ src: "foo" }
{ src: "foo" }

Actual result

Developer console:

{ src: "foo" }
{ src: "foo\n" }

Or maybe the bug is that in preview mode there isn’t a newline, anyhow the source passed to the processor differs.

I’ve had a bug report for the Tasks plugin that looks like it is caused by this behaviour (different search results in Live Preview than Reading mode):

Is it known which version of Obsidian introduced this behaviour?

My user is speculating the cause may be the following change in 1.7.4

I think perhaps the stable/public release of Obsidian 1.7.4 is the culprit, which includes the Catalyst/preview changes from Obsidian 1.7.2, judging by the changelog entry Live preview now only escapes special characters (not letters and numbers)..

1 Like

I will look into this. We’ve recently made some changes to fix html entity characters in source code that wasn’t properly decoded, so that’s likely where the bug came from.

1 Like

will be fixed 1.7.5

1 Like

Thank you for the quick fix!

Just to note that the title of this issue - extra newline when in reading mode - is different from the actual change in behaviour…

From Tasks bug #3137, the observable change in behaviour is that the newline that used to be present at the end of code blocks in Live Preview is now removed…

I guess it makes sense following the unix convention that a line ends with \n.
Does really matter for me as I now trim whitespace and add a single newline at the end myself.