Search

Hi, I try to output a correct json format from a markdown field without success because of line breaks. The markdown field in the datasource looks like this:

<data>
<entry>
<text mode="formatted"><p>My first article texte <strong>with</strong><em>styles</em>  
and with a line break</p>
</text>
</entry>
<data>

There is a line break in the datasource. The xsl to output the json is a <xsl:copy-of select="text/*" /> but it keeps the line break, in the resulting json so it s not valid. The node-set of text needs to be one single line.

I tried many things, xml:output indent=no, no result. Replacing carriage returns with br in the string works, but remove all other html tags (as well as normalize-space(), which output a single line as if it was a value-of and not a copy-of).

Any idea about how to return a node-set without any line break?

Have a nice day.

@vincentd - Have you tried using the <xsl:strip-space /> element?

Here's more info on whitespace handling in XSLT.

During painful hours I tried many things, if I remember well strip space remove important spaces too ex: between bold and em tags. Having a break now I think I didn't try one thing yet: Converting the node set to an escaped string, then replace the line break with a space. And finally test the js in front end to see if he can decode the json with escaped html instead of raw html. But this is a lot of xsl operations so perhaps a text area with ckeditor could be better. I will test and come back.

I wonder why commonmark/mardown parsers keeps the line breaks in the rendering... :( if a line break could be nothing (or a space) as it seems it should be I could have save an afternoon.

I wonder why commonmark/mardown parsers keeps the line breaks in the rendering

So are you using CommonMark? Or Markdown Extra?

Yes editorforsymphony to help markdown syntax of the textarea and commonmark text formatter. I also tried previous version of editorforsymphony with markdown extra.

Got it. So if you need to convert a html from markdown to json you will probably need to remove the extra line breaks. Don't waste your time with xsl for that, you can change the text formatter function, common marks here, replace:

    public function run($string)
{
    return trim($this->converter->convertToHtml($string));
}

with:

    public function run($string)
{
    $string = trim($this->converter->convertToHtml($string));
    $string = preg_replace('/[/r/n]+/',' ', $string); <!-- replace all "/" between [ ] with backslashes (impossible to make the backslash visible here) -->
    return $string;
}

This way your formatted value of the field in the datasource won't have any line break. Here the line breaks are replaced with a space.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details