New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown code-block indentation removed when saving as field #1567

Closed
CodeAndWeb opened this Issue Aug 26, 2017 · 16 comments

Comments

Projects
None yet
8 participants
@CodeAndWeb

CodeAndWeb commented Aug 26, 2017

Expected behaviour

When using code blocks in markdown fieldtype the indentation should be retained.

Actual behaviour

All trailing spaces in a code block are removed when using markdown field set. Manually editing the YAML file works - everything is rendered as it should be. But as soon as you open the page in the control panel and save it the spaces are removed:

The YAML file

type: markdown
content: |
  Code:
  
  ``` language-js
  function windowLoad(context){
  if(mr.status.documentReadyRan){              <---- this should be indented
  mr.status.windowLoadPending = false;
  component(context);
  });
  }
  ```

instead of

type: markdown
content: |
  Code:

  ``` language-js
  function windowLoad(context){
        if(mr.status.documentReadyRan){
              mr.status.windowLoadPending = false;
              component(context);
        });
  }
  ```

Steps to reproduce

Create a fieldset containing markdown.
Create a page using this fieldset.

Paste some code block with indentation.
Save.

Server Details

Operating System: Mac

Web Server: PHP internal

PHP Version: 5.6

Statamic Version: 2.6.4

Updated from an older Statamic or fresh install: From 2.6.3

@CodeAndWeb

This comment has been minimized.

CodeAndWeb commented Aug 26, 2017

The problem is located in the spyc YAML dumper and parser: It modifies the data before storing it:
Spaces are trimmed and quote marks (") at the beginning and end of each line are removed.
The parser also trims all strings when loading data.

This is something that in my opinion must not happen: I'd expect to get the same data when saving and loading it.

I've tested replacing the YAML module with https://github.com/symfony/yaml and it seems to do a way better job.

Changing YAML.php to use

use Symfony\Component\Yaml\Yaml as SYMYAML;

$yaml = SYMYAML::dump($data,100, 2, SYMYAML::DUMP_MULTI_LINE_LITERAL_BLOCK);

for writing and parsing with

SYMYAML::parse($str)

keeps the Markdown as it's written without destroying the data.

@FrittenKeeZ

This comment has been minimized.

FrittenKeeZ commented Aug 28, 2017

I have the same problem when using a markdown field in a replicator set, where the line endings are trimmed - ergo losing line breaks.

@jasonvarga

This comment has been minimized.

Member

jasonvarga commented Aug 28, 2017

We are aware of the issue in Spyc. See mustangostang/spyc#54

The next version of Statamic will have an updated version of the Markdown parser which has an option for hard line breaks (Github style). If you enable that, you can avoid the requirement for two spaces at the end for a line break. This could be a nice workaround until the Spyc bug is fixed.

@CodeAndWeb

This comment has been minimized.

CodeAndWeb commented Aug 29, 2017

@jasonvarga I've seen this isuue - but this one is about parsing the fields. The bug I am refering go is about writing the data. In fact it's mangled twice. The issue is also not describing the problems with lines wrapped in quote marks - which simply get lost, too...

You've submitted this bug 1 year ago to spcy - there was no update, no response - nothing. The last update to the parser was 6 months ago. I would really recommend switching to a better supported and tested component.

As already mentioned: https://github.com/symfony/yaml is well tested, gets regular updates and improvements.

@jackmcdade

This comment has been minimized.

Member

jackmcdade commented Aug 29, 2017

The problem with Symfony's component is that it's a different YAML spec 1.2). If we just swapped packages, everybody's sites will break because of syntax strictness. It's a tough spot we're in.

@FrittenKeeZ

This comment has been minimized.

FrittenKeeZ commented Aug 29, 2017

I haven't had any problems with Symfony's component. Apart from wrapping all non-single word strings in quotations, I haven't seen anything out of the ordinary.

@jackmcdade

This comment has been minimized.

Member

jackmcdade commented Aug 29, 2017

If you don't have a space next to the colon, it throws exceptions. For a platform that can be editing without the control panel, this is a super common occurrence.

var: value #fine in Symfony
var:value #totally broken in Symfony

Also (and while this is a benefit, it is a breaking change), truthy/falsy rules are quite different. y, n, yes, no, etc.

@jasonvarga

This comment has been minimized.

Member

jasonvarga commented Aug 29, 2017

Ignore my comment about the line breaks. Looks like I misunderstood the issue. My bad.

@CodeAndWeb

This comment has been minimized.

CodeAndWeb commented Nov 2, 2017

Any updates on this? It's still broken in 2.7.1

I'd really like to use statamic - but this one is a complete show stopper for me.
I need working code blocks that keep their indentation.

@enwin

This comment has been minimized.

enwin commented Feb 27, 2018

Sorry to be the guy posting just to get a status of this issue but I recently started to use the markdown fieldtype in a bard set to display code blocks and I'm facing the same issue. Is there a fix planned for the 2.8.x version of Statamic?

Cheers

@sicbe

This comment has been minimized.

sicbe commented Mar 20, 2018

I'm having the same problem. I'm building a website based around a lot of code blocks (tutorials for students) so it's really annoying.

Could there be an option so you can choose which parser you use? People who have problems with the different syntax could use the older one, and people who use the correct syntax could use the Symfony one?

@mikemartin

This comment has been minimized.

mikemartin commented Mar 21, 2018

To clarify, code snippets maintain their indentation when using the main content field but lose their indentation when stored in Replicator or Bard sets.

I've found a workaround for this that involves wrapping the whitespace in quotes:

"$(function() {"
"	// Automatically add Zoom interaction"
"	$('.markdown img').attr('data-action', 'zoom');"

outputs:

code: |
      $(function() {
      	// Automatically add Zoom interaction
      	$('.markdown img').attr('data-action', 'zoom');

@jasonvarga @jackmcdade Just a thought, but could we create a new Code or Pre fieldtype that wraps the whitespace in quotes before writing to the yaml file?

@jasonvarga

This comment has been minimized.

Member

jasonvarga commented Mar 21, 2018

We're going to just swap out the YAML parser with Symfony, which I believe doesn't suffer from this problem.

@smaida

This comment has been minimized.

smaida commented May 17, 2018

Is there an ETA for this?

@jasonvarga

This comment has been minimized.

Member

jasonvarga commented May 17, 2018

Yes, expect it for next release.

@jasonvarga

This comment has been minimized.

Member

jasonvarga commented May 17, 2018

New installs from 2.9 onwards will use Symfony's YAML parser by default.

Upgrades to 2.9 will use Spyc for backwards compatibility, but you will be able to just add yaml_parser: symfony to system.yaml to switch it back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment