-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
add Markdown.postwalk and prewalk #54224
base: master
Are you sure you want to change the base?
Conversation
Imagine if we had totally general "walks" in Julia that don't require manual reimplementation for any specific case – be it Markdown or Expr... :) Of course here we are talking about Base and suggesting packages is off-topic, but still find it nice. |
Totally, I'm not saying that generic walks is an immediate solution to all problems. The cleanest way here would be to have a Anyway, I personally don't have anything against these specialized methods here. No way around that if they are needed by something in Base. |
Regardless, I think we should have |
See also MarkdownAST.jl which could be an alternative for IJulia. |
Yes, I would also recommend converting Markdown -> MarkdownAST and then using AbstractTrees to walk. I don't think we want to add any more features to the Markdown stdlib. We should really consider it deprecated. |
That's not going to happen in the foreseeable future (i.e. pre-Julia 2.0), because it's used by default for docstrings, and in general is used extensively throughout the whole Julia ecosystem. As such, it would be a huge mistake IMO to treat the Markdown stdlib as if it were abandonware. |
That's why we can't remove it pre-2.0. But we can deprecate it, -ish. Anything new can be built on top of MarkdownAST, which can losslessly convert from the Markdown stdlib representation, and already provides treewalking etc. via AbstractTrees, and is in general easier to contribute to and extend, since it's not tied to Julia releases etc. And any existing users of the Markdown stdlib should transition away from directly using it. (My two cents.) |
This PR adds
Markdown.postwalk
andprewalk
functions forMarkdown.MD
expressions based on the corresponding functions in MacroTools.jl. This lets you do simple transformations of Markdown expressions without knowing much about the internals of theMarkdown
module.For example, with
which produces:
You can transform
txt
to uppercase with:which produces
Note that only "plain text" appears as
String
arguments to thepostwalk
function, not code or URLs or other text with a technical meaning. You can also see how thepostwalk
function traverses the expression with, for example:My motivation here is that I really wanted to lower the "heading level" when displaying docstrings in IJulia (JuliaLang/IJulia.jl#766 … see also #22870), but this transformation is impossible to implement without a
postwalk
-like function that requires lots of knowledge of the internals of the Markdown module, and it seems to me that this belongs in the Markdown package itself.Currently,
prewalk
andpostwalk
are not exported (using them effectively still might require some knowledge of internals, e.g. of the specific node type you want to transform).Needs tests and NEWS, but I wanted to check what people think first.