Skip to content
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

YamlFrontMatterBlock rendered as markdown does not preserve nested lists #293

Closed
3 tasks done
olafurpg opened this issue Jan 5, 2019 · 4 comments
Closed
3 tasks done

Comments

@olafurpg
Copy link

olafurpg commented Jan 5, 2019

I am using Parser and Formatter to transform markdown documents and produce markdown documents. The problem is that YAML frontmatter when using YamlFrontmatterExtension with nested lists are not preserved when rendering back into markdown.

  • Parser
  • Formatter
  • YamlFrontmatterExtension

A frontmatter block like this

---
linters:
  - name: linter
    rules:
      - name: UseIsNanNotNanComparison
        url:  https://github.com/HairyFotr/linter/blob/master/src/test/scala/LinterPluginTest.scala#L1930
---

renders into the following output

---
linters:
- name: linter
rules:
- name: UseIsNanNotNanComparison
url:  https://github.com/HairyFotr/linter/blob/master/src/test/scala/LinterPluginTest.scala#L1930
---

I expected the rendered output to be the same as the input.

Minimal reproduction code example in Scala.

    val settings = new MutableDataSet()
      .set(Parser.EXTENSIONS, List(YamlFrontMatterExtension.create()).asJava)
    val parser = Parser.builder(settings).build()
    val formatter = Formatter.builder(settings).build()
    val markdown =
      """|---
         |linters:
         |  - name: linter
         |    rules:
         |      - name: UseIsNanNotNanComparison
         |        url:  https://github.com/HairyFotr/linter/blob/master/src/test/scala/LinterPluginTest.scala#L1930
         |---
      """.stripMargin
    val parsedMarkdown = parser.parse(markdown)
    val renderedMarkdown = formatter.render(parsedMarkdown)
    assertNoDiff(renderedMarkdown, markdown)

A workaround is to unlink the frontmatter block and prepend it's chars to rendered output.

    val appendable = new java.lang.StringBuilder()
    document.getChildren.asScala.headOption match {
      case Some(f: YamlFrontMatterBlock) =>
        appendable.append(f.getChars)
        f.unlink()
      case _ =>
    }
    formatter.render(document, appendable)
    appendable.toString
@vsch
Copy link
Owner

vsch commented Jan 7, 2019

@olafurpg, I missed setting pre-formatted for yaml block so whitespace is preserved.

I also added first level values as child nodes of yaml node so proper offsets in file can be determined.

  • Fix: YamlFrontMatterBlock rendered as markdown does not preserve nested lists #293, YamlFrontMatterBlock rendered as markdown does not preserve nested lists
    • Add: YamlFrontMatterValue node containing yaml value(s) inserted as children of
      YamlFrontMatterNode
    • Fix: change stored key as BasedSequence instead of string, can be retrieved as YamlFrontMatterNode.getKeySequence()
    • Add: code to return List<String> from child nodes of YamlFrontMatterNode
    • Fix: resolve offsets in YamlFrontMatterNode and YamlFrontMatterValue nodes.

The changes do not properly parse list values, this is a limitation of the original yaml parser implementation and without rewriting it for full Yaml syntax cannot be fixed. I would not recommend using the block key/values parsed for anything but simple values.

@vsch vsch added this to the Version 0.4.0 milestone Jan 7, 2019
@olafurpg
Copy link
Author

I am OK with the limitation that YamlFrontMatterNode doesn't support all of YAML (it's a huge spec!). This issue is primarily about preserving the original formatting of the frontmatter block when the YamlFrontMatterNode is unchanged. In my particular use-case, I only transform code fences.

@vsch
Copy link
Owner

vsch commented Jan 11, 2019

@olafurpg, the issue is fixed in 0.40.4.

@vsch vsch closed this as completed Jan 14, 2019
@olafurpg
Copy link
Author

Thank you @vsch I am unable to reproduce the bug in 0.40.4, great job!

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

No branches or pull requests

2 participants