Retaining spaces

When the user types multiple spaces, then these spaces are shown by ProseMirror. However, unless I am making a mistake, it seems that when converting to HTML and back, multiple spaces are collapsed into a single space (or no space at all at the beginning of a paragraph). Is there a way around this? Thanks!

EDIT: I found this: 1 This might solve my problem.

There’s a certain ambiguity in interfacing with HTML – HTML often contains extra whitespace that shouldn’t be displayed (around tags, for indentation, etc). So our HTML parser collapses that. However, you sometimes do want trailing, leading, or multiple space in a document, at least during editing, hence the special parser mode that preserves whitespace.

Our current approach is a little confused (parse the initial document dropping whitespace, but preserve it during editing), and I’ll have to think about this some more at some point.

Ok. Indeed it is confusing :smile:

By the way, I’ve found that setContent does not take an options parameter, so that preserveWhitespace cannot be conveniently set.

Instead, I am now using the following code:

    var x = document.createElement("div");
    x.innerHTML = the html that contains extra spaces;
    var opts = {};
    opts.preserveWhitespace = true;
    var node = from_dom.fromDOM(pm.schema, x, opts);

For a more succinct approach, you can use

pm.setContent(format.fromHTML(pm.schema, "my html", {preserveWhiteSpace: true}))

(src/format is intended to be imported as a whole, rather than by individual submodule).

1 Like