Coder Social home page Coder Social logo

Comments (6)

tlienart avatar tlienart commented on August 24, 2024 1

Cool thank you! I'll use your trick

from commonmark.jl.

MichaelHatherly avatar MichaelHatherly commented on August 24, 2024 1

That fix with be tagged shortly: 438032d.

I'll note here that I'd regard what I used to make that trick work can be regarded as public interface and won't be disappearing at random. The previous trick with push! was definitely "doing bad things".

from commonmark.jl.

MichaelHatherly avatar MichaelHatherly commented on August 24, 2024

be able to ignore the conversion of indented blocks

The spec does bake-in a little bit with regards to indentation as far as I know, but on trying to disable indented code it doesn't appear to break much. So might be possible to achieve that fairly easily.

julia> p = CommonMark.Parser();

julia> text =
       """
           indented
       """
"    indented\n"

julia> ast = CommonMark.parse(p, text)
Node(CommonMark.Document)

julia> CommonMark.ast_dump(ast)
Document
  CodeBlock
    "indented\n"

julia> pop!(p.block_starts)
indented_code_block (generic function with 1 method)

julia> ast = CommonMark.parse(p, text)
Node(CommonMark.Document)

julia> CommonMark.ast_dump(ast)
Document
  Paragraph
    Text
      "indented"

Whether disabling it causes odd behaviour elsewhere I couldn't say for certain just yet. Would be worth trying out though.

I would absolutely love to get rid of indented code blocks completely from the syntax, but alas that's never going to be a realistic goal, it makes parsing quite a bit more complex than it needs to be. https://johnmacfarlane.net/beyond-markdown.html was good reading while putting this package together.

(minor thing) {{...}} are converted by the Markdown module into html entities

Those aren't converted. I thought for a moment that I might have implemented that wrong, but babelmark doesn't either if I'm not mistaken.

If those are the things you'd need to get going then you might have some success, hopefully!

from commonmark.jl.

tlienart avatar tlienart commented on August 24, 2024

Thanks for the code, awesome I look forward to trying this out over the weekend :)

from commonmark.jl.

tlienart avatar tlienart commented on August 24, 2024

Sorry for briefly re-opening this, the trick you suggested does not work anymore but this:

CommonMark.indented_code_block(::CommonMark.Parser, ::CommonMark.Node) = 0

seems to work fine. It preserves base markdown formatting that happens in the indentation for instance:

julia> p = CommonMark.Parser();
julia> t = """
       ab **and**

           cd
           ef **gh**
               foo `bb`
           bar

       end"""
"ab **and**\n\n    cd\n    ef **gh**\n        foo `bb`\n    bar\n\nend"

julia> CommonMark.ast_dump(p(t))
Document
  Paragraph
    Text
      "ab "
    Strong
      Text
        "and"
  Paragraph
    Text
      "cd"
    SoftBreak
      ""
    Text
      "ef "
    Strong
      Text
        "gh"
    SoftBreak
      ""
    Text
      "foo "
    Code
      "bb"
    SoftBreak
      ""
    Text
      "bar"
  Paragraph
    Text
      "end"

apart from the fact that it's a dirty trick, do you see anything that could go terribly wrong from the CommonMark.jl perspective if I use this? thanks!

from commonmark.jl.

MichaelHatherly avatar MichaelHatherly commented on August 24, 2024

Couldn't say with any certainty whether that will do anything terribly bad, worth trying it out, though parsing CM can be pretty weird at times. You can avoid the type piracy on that method (I assume you're redefining it within the REPL (and Franklin)? by doing something like the following:

struct SkipIndented end
block_rule(::SkipIndented) = Rule((p, c) -> 0, 8, "")
julia> p = enable!(disable!(Parser(), IndentedCodeBlockRule()), SkipIndented())
Parser(Node(CommonMark.Document))

This swaps out the indented block parser with your definition without the piracy. (I was surprised that it didn't actually break anything at all.)

julia> CommonMark.ast_dump(p(t))
Document
  Paragraph
    Text
      "ab "
    Strong
      Text
        "and"
  Paragraph
    Text
      "cd"
    SoftBreak
      ""
    Text
      "ef "
    Strong
      Text
        "gh"
    SoftBreak
      ""
    Text
      "foo "
    Code
      "bb"
    SoftBreak
      ""
    Text
      "bar"
  Paragraph
    Text
      "end"

This needs a tiny patch to CM, since there's a bug in disable! that this uncovered (which I'll push tomorrow):

diff --git a/src/parsers/rules.jl b/src/parsers/rules.jl
index a20a582..2626c3e 100644
--- a/src/parsers/rules.jl
+++ b/src/parsers/rules.jl
@@ -50,7 +50,7 @@ function disable!(p::AbstractParser, rules::Union{Tuple, Vector})
     empty!(p.inline_parser.inline_parsers)
     empty!(p.inline_parser.modifiers)
     filter!(f -> f ∉ rules, p.rules)
-    return enable!(p, p.rules)
+    return enable!(p, copy(p.rules))
 end
 disable!(p::AbstractParser, rule) = disable!(p, [rule])

from commonmark.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.