-
Notifications
You must be signed in to change notification settings - Fork 63
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
Reference Style Links are Converted (and lost) #34
Comments
Hi @tajmone, thank you for writing up this detailed issue. I will reply to various parts of your comment.
First thing to keep in mind about
I should elaborate, but what I mean by that is... Like When I first created Later on, I realized that Markdown is too vague of a "specification" and has too many variations and inconsistencies for this to be possible, so I know it's not an attainable goal. As I said in shurcooL-legacy/atom-markdown-format#26 (comment), If I really wanted to achieve that today, I'd begin by creating a standalone spec first. However, this is just background information. Let me get into this specific issue.
Thanks for doing that, it's very helpful.
Thank you for providing that rationale. It's convincing, and I understand why it would be preferable to maintain the two type of links separately instead of merging them into the same inline kind. I agree. 👍 Now let's discuss the potential implementation side of this.
The current blackfriday implementation is very old, and has certain limitations that are unavoidable. One of them is that it does not preserve the original markdown document layout, and it's a very lossy parsing. It only emits enough information to produce HTML for the rendered Markdown. So, it's actually extremely hard to implement this with that blackfriday implementation, even if I really wanted to. You can also see #6 (comment) for a detailed explanation. However, there is a version 2 of blackfriday which resolves this limitation. If |
@shurcooL , thanks for the great reply. I personally don't see any issue wheather markdownfmt uses I wasn't aware of this blackfriday limitations, nor that a version 2 is available. Thanks for letting me know, and for having accepted this proposal as an enhancement. Wish I could help, but right now my knowledge in Go is too weak ... and I already juggling with a few langs I'm learning. B.R. Tristano |
@shurcooL, first thanks for sharing this tool. I'd also like to see this behavior change. Is there an active effort to move to v2 of blackfriday? |
I'm not a fan of @JStenberg has a fork of markdownfmt that uses blackdown.v2, which the earlier comments in this thread suggested has sufficient hooks to enable rendering reference links. I've been reading through the blackfriday source and running some dumb tests w/ Here is the bit where they're parsed. Reference links ( Heavy handed use of case Link:
// mark it but don't link it if it is not a safe link: no smartypants
spew.Dump(node.LinkData)
dest := node.LinkData.Destination
if needSkipLink(r.Flags, dest) { I've tentatively concluded that to get the behavior I want I'd need to convince blackfriday to use two different types of Nodes and the modify the renders. Does that match your understanding? |
See issue #39 and PR #40 for the latest updates on that.
It was my understanding that blackfriday v2 would preserve the information about whether the link was inline or reference-style, as preserving more information about the parsed markdown document was a part of the design goal of v2. I hadn't actually confirmed whether that's the case for this particular case. /cc @rtfb Do you know if v2 lets one be able to tell whether a link was inline or reference-style? |
I don't know about the inline vs reference-style, but v2 of blackfriday isn't able to tell you whether a link is an auto link or not. So you can't tell the difference between these: |
I'm 99.9% sure that blackfriday.v2 is discarding the information about the type of link used and collapsing it into the same node for either case. Here is the code that's doing the work. The fastest hack to peek at what was happening was to add a call to case Link:
// mark it but don't link it if it is not a safe link: no smartypants
spew.Dump(node.LinkData)
dest := node.LinkData.Destination
if needSkipLink(r.Flags, dest) { and then run a little test program. package main
import (
"fmt"
"gopkg.in/russross/blackfriday.v2"
)
func main() {
input := []byte(`
- This is [reference][moose]
- This is [inline](http://www.georgehartzell.com)
[moose]: http://www.georgehartzell.com
`)
output := blackfriday.Run(input)
fmt.Println(string(output))
} It generates the following output (dump is called twice, explanation here):
|
Thanks for digging into this, @hartzell! You're right, currently Blackfriday does not record this information. Here's a tracking issue for that: russross/blackfriday#369 (not exactly the same issue, but close enough to clump together). |
I wanted to carry on from issue #26 (now closed).
markdownfmt converts reference-styled links into normal inline links. Eg:
becomes:
I think this is quite an issue. A tidy-up tool shouldn't convert these links and remove the reference labels — tidy-markdown doesn't.
I would like to motivate this issue through real-use scenarios examples.
It is common practice when working with markdown documentation to store all links at the end of a document, in reference-link sections. This means that when a link changes, one only has to go at the bottom of all documents and change the links section and all links within the document will be «updated» (so to speak).
To give you an example: using RawGit to provide html live previews — the proper way to use RawGit for production links is to use the
cdn.rawgit
URL, which caches permanently the preview. So, usually links point to specific commits of the file, to prevent permanent caching of themaster
branch version. As files change in time, so do their RawGit preview URLs:after a new commit (hash:
xyz1938
) might become:And so on with all links that use RawGit — the commit part of the URLs will have to change to accomodate the new commit hash.
And instead of going mad to search-and-replace all URLs inside the actual links, the quick way is to change it just in the links-section at the end of each document in the repo — something often automated with scripts in large documentation projects.
Basically, markdown makes life a lot easier with reference-styled links — I personally store some snippets files with all the links used in a project, carry out all changes to links in the snippet files, and then just copy-and-paste the whole links-section over the old one in all docs (yep: ALL DOCS, because unused link-ids are harmless, they just don't show up in the final preview of the document!).
In bigger projects, I use a pandoc toolchain, and invoke pandoc to always add/include the reference-links section file when processing sources — so the actual link-section is stored in a single file, used by all documents for all external links. This is a cool way to handle massive quantities of links. And it also allows to have different link-sections for different versions of a project (eg for a repo which is both on GitHub and Bitbucket) without having duplicate documents.
But Why?
What I haven't yet figure out is why blackfriday converts these links. Usually this happens in parsers which built an AST from the source (like pandoc), and in the post-processing phase they replace all references with their actual values. But I haven't yet worked my way into the source code enough to understand how it all works under the hood.
If this was the case, it might not be so easy to change this behavior. Any ideas about it?
But definitely, a clean-up tool shouldn't remove elements from the doc.
The text was updated successfully, but these errors were encountered: