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

Use custom parser for markdown processing. #198

Merged
merged 3 commits into from
Oct 30, 2019
Merged

Conversation

olafurpg
Copy link
Member

@olafurpg olafurpg commented Oct 29, 2019

Previously, mdoc used the flexmark library for processing markdown
files. Since flexmark is a proper markdown parser, it may not always
understand the format of the document that mdoc was processing, for
example:

  • revealjs slides inside HTML files
  • custom markdown syntax that is not supported by commonmark

This commit introduces a new custom parser that only understands the
parts of markdown files that mdoc touches: code fences. Since mdoc
doesn't need to understand all markdown flavors to process code fences,
we can get away with a much simpler parser than flexmark.

With this change, it should be possible to use mdoc on any document as
long as the code fences start at the beginning of a line with the
triple-backtick "scala mdoc" syntax

```scala mdoc
println(42)
```

Everything outside the code fences is left unchanged,
character-by-character, so the underlying markup language is
preserved.

Previously, mdoc used the flexmark library for processing markdown
files. Since flexmark is a proper markdown parser, it may not always
understand the format of the document that mdoc was processing, for
example:

* revealjs slides inside HTML files
* custom markdown extensions that are not supported by commonmark

This commit introduces a new custom parser that only understands the
parts of markdown files that mdoc touches: code fences. Since mdoc
doesn't need to understand all markdown flavors to process code fences,
we can get away with a much simpler parser than flexmark (that
understands all of markdown).

With this change, it should be possible to use mdoc on any document as
long as the code fences start at the beginning of a line with the
triple-backtick "scala mdoc" syntax
````
```scala mdoc
println(42)
```
````

Everything outside the code fences is left unchanged,
character-by-character, so the underlying markup language is
preserved.
@olafurpg
Copy link
Member Author

There are still several failing test suites

[error] Failed tests:
[error] 	tests.markdown.SilentSuite
[error] 	tests.markdown.MarkdownFileSuite
[error] 	tests.markdown.ErrorSuite
[error] 	tests.markdown.ResetSuite
[error] 	tests.cli.ScalacOptionsSuite
[error] 	tests.markdown.FailSuite
[error] 	tests.markdown.ScastieModifierSuite
[error] 	tests.markdown.ToStringSuite
[error] 	tests.markdown.SiteVariableInjectorSuite
[error] 	tests.markdown.CompileOnlySuite
[error] 	tests.cli.CliSuite
[error] 	tests.markdown.JsSuite
[error] 	tests.markdown.MultiModsSuite

@olafurpg
Copy link
Member Author

FYI @tpolecat this should unblock some tut users from migrating to mdoc. My understanding is that a common use-case is to run tut on HTML files with Reveal.js slides, is that correct? Are there any test cases I could add to ensure this use-case is covered now?

@tpolecat
Copy link
Contributor

@olafurpg yes, the remark.js use case is the main thing holding people back. I don't have any specific testcases for it because tut ignores everything outside fenced blocks.

This is great!

@olafurpg olafurpg marked this pull request as ready for review October 30, 2019 09:55
@olafurpg
Copy link
Member Author

The only regression from this change is that we no longer expand site variables in the ![version] syntax. Users should use @VERSION@ instead. This feature was never documented on the website and it's not used in any of my own documentation so I will let just mention it in the release notes.

|```
|
|```scala
|implicit val x: Int = 41
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hmf Can you elaborate on why this test was passing before? The mdoc:reset modifier should reset the scope so the println(x) should not compile. I updated the test case to introduce a new implicit val x value

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context #192

@olafurpg
Copy link
Member Author

Let's give this a try. The failing tests mostly highlighted a lot of cases where flexmark was introducing blank lines that didn't exist in the original source.

@olafurpg olafurpg merged commit 33b19a7 into scalameta:master Oct 30, 2019
@olafurpg olafurpg deleted the parse branch October 30, 2019 10:49
@olafurpg
Copy link
Member Author

1.4.0-RC2 is out with this change!

cc/ @julien-truffaut iirc, you were unable to use mdoc with reveal.js, could you maybe give it a try again with this release?

@julien-truffaut
Copy link

@olafurpg Thanks let me give it a try

@olafurpg
Copy link
Member Author

The file still needs to have the *.md file extension. We could change mdoc to process *.html files if that helps, I'm happy to iterate on this.

@julien-truffaut
Copy link

@olafurpg It works! it would be perfect if mdoc could also process html files.

Here is an example: fp-tower/scala-intro#1

@olafurpg olafurpg mentioned this pull request Oct 30, 2019
@olafurpg
Copy link
Member Author

Awesome! I'll update it to process *.html then just like it already does for *.md files

@olafurpg
Copy link
Member Author

@julien-truffaut 1.4.0-RC3 should process *.html files the same way it processes *.md files

@julien-truffaut
Copy link

Awesome. Thanks a lot @olafurpg

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

Successfully merging this pull request may close these issues.

None yet

3 participants