element-hq / element-android

A Matrix collaboration client for Android.
https://element.io/
GNU Affero General Public License v3.0
3.35k stars 711 forks source link

Code blocks collapse spaces destroying whitespace-sensitive code #8153

Open HarHarLinks opened 1 year ago

HarHarLinks commented 1 year ago

Send a code block that contains some whitespace-sensitive code. This could be something serious such as a Python snippet, which becomes impossible to interpret, other indented code which merely becomes illegible, or fun things such as the ASCII art in the following example.

Steps to reproduce

Send a message using:

Element Nightly version: 2023022001 Olm version: 3.2.12

image

Outcome

What did you expect?

match desktop

What happened instead?

all spaces at the start of a line are collapsed, except the first line image

Your phone model

No response

Operating system version

13

Application version and app store

1.5.25 F-Droid

Homeserver

synapse 1.77.0

Will you send logs?

No

Are you willing to provide a PR?

No

MichaelSasser commented 1 year ago

Rendering the whitespace in front of a line (indentation) is broken in Markdown code blocks for quite some time now. Same with syntax highlighting. It is impossible to read or understand Python code, without viewing the raw event, as it can be interpreted differently otherwise.

{
  "content": {
    "body": "```rust\n#[derive(Debug, FromRow, Clone)]\npub struct RoomModel {\n    pub tenant_id: i32,\n    pub room_id: String, // pk\n    pub room_type: RoomModelType,\n    pub room_shortcode: Option<String>\n}\n\nimpl RoomModel {\n    pub fn new(\n        tenant_id: i32,\n        room_id: String,\n        room_type: RoomModelType,\n        room_shortcode: String,\n    ) -> Self {\n        Self {\n            tenant_id,\n            room_id,\n            room_type,\n            room_shortcode,\n        }\n    }\n```",
    "format": "org.matrix.custom.html",
    "formatted_body": "<pre><code class=\"language-rust\">#[derive(Debug, FromRow, Clone)]\npub struct RoomModel {\n    pub tenant_id: i32,\n    pub room_id: String, // pk\n    pub room_type: RoomModelType,\n    pub room_shortcode: Option&lt;String&gt;\n}\n\nimpl RoomModel {\n    pub fn new(\n        tenant_id: i32,\n        room_id: String,\n        room_type: RoomModelType,\n        room_shortcode: String,\n    ) -&gt; Self {\n        Self {\n            tenant_id,\n            room_id,\n            room_type,\n            room_shortcode,\n        }\n    }\n</code></pre>\n",
    "msgtype": "m.text"
  },
  "origin_server_ts": 1678458276458,
  "sender": "@michael:michaelsasser.org",
  "type": "m.room.message",
  "unsigned": {
    "age": 297
  },
  "event_id": "$ao2yAYcPI1kaNLgoR5UAlSQMCzcmvG2SyyAwwc5EZ3U",
  "room_id": "!huuCJnEbajPPGJXgbt:michaelsasser.org"
}

Untitled-1

(Adding the missing } doesn't make a difference. I've just copied whatever I was working on at the moment as an example)

HarHarLinks commented 1 year ago

Thank you Michael, I'm updating the OP since that seems much more serious than just ASCII art

theIDinside commented 1 year ago

I've identified this (I think) to be a problem in one of the many dependencies this project unfortunately rely on. It's in the commonmark library (which the dependency Markwon depends on), and as you can see, it "collapses" whitespace (rather, it eats whitespace that is at the beginning of each line).

I've sent a PR for a fix on that to the repo, but as I don't really have much insight into that codebase, I don't know if it will actually be approved.

Is there any work being done in lifting out the work from 3rd party libraries into the element-android code base? That would solve this problem, but I have a sneaking suspicion that due to the substantial reliance on 3rd party libraries a lot of other features could potentially also be blocked behind the fact that some library not under this project's control has made some API decision that makes feature X, just impossible.