Skip to content

Conversation

glebec
Copy link
Contributor

@glebec glebec commented Sep 21, 2018

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Documentation Update

Description

Enables the with_toc_data option for the Redcarpet markdown renderer. This adds id attributes to the markdown headers, allowing for hash links to specific page content. This is useful both for linking and for generating tables of content (ToCs).

Input:

# Title

Text

## More

Etc.

Rendered (before):

<h1>Title</h1>
<p>Text</p>
<h2>More</h2>
<p>Etc.</p>

Rendered (after):

<h1 id="title">Title</h1>
<p>Text</p>
<h2 id="more">More</h2>
<p>Etc.</p>

NB, I don't know Ruby so you should definitely sanity-check the code and make sure it makes sense w.r.t. how these classes & functions are intended to work.

Related Tickets & Documents

Might be considered to close #183. However, you might also want a feature similar to GitHub's markdown renderer, which makes it clear that headers have anchors via a link or hover icon.

Mobile & Desktop Screenshots/Recordings (if there are UI changes)

Added to documentation?

  • docs.dev.to
  • readme
  • no documentation needed

@glebec glebec changed the title feat(markdown): add ids to headers WIP feat(markdown): add ids to headers Sep 22, 2018
@bennypowers
Copy link
Contributor

I'd like to try my hand at getting those tests passing. @glebec are you comfortable adding me as a contributor to your fork?

@glebec
Copy link
Contributor Author

glebec commented Sep 27, 2018

Please do – I was probably going to post that anyone who wanted to take a crack at this branch should feel free, as I don’t have a strong need for this feature currently myself.

@Zhao-Andy
Copy link
Contributor

Zhao-Andy commented Sep 27, 2018

Hey @bennypowers and @glebec, this is very late notice (apologies), but I have a PR for this issue for this in our old repo that is mostly done. I'll bring that PR over since I've worked out most of the bugs and kinks that come along the way.

If you'd like, you can keep working on this PR, but I think it makes sense for us to take a look at both PRs and see which one should be merged into the other. Sorry for letting the crossed wires happen!

@glebec
Copy link
Contributor Author

glebec commented Sep 27, 2018

@Zhao-Andy no worries, I haven't spent any serious time or thought on this PR – nor do I know this codebase (or even language) well. So I'm going to assume a priori that your branch is probably going to be farther along. We can certainly review that, of course.

@bennypowers
Copy link
Contributor

@Zhao-Andy in that case I'll finish up with my current test and fire it off for comparisson. Thanks!

@bennypowers
Copy link
Contributor

I got bored so I fixed the tests ;)

@@ -54,7 +54,7 @@ def generate_and_parse_markdown(raw_markdown)
it "does not generated nested link tags" do
nested_links = generate_and_parse_markdown("[[](http://b)](http://a)")
nested_links = Nokogiri::HTML(nested_links).at("p").inner_html
expect(nested_links).to eq('[<a href="http://b" rel="noopener noreferrer"></a>](<a href="http://a" rel="noopener noreferrer">http://a</a>)') # rubocop:disable Metrics/LineLength
expect(nested_links).to eq('[<a href="http://b"></a>](<a href="http://a">http://a</a>)')
Copy link
Contributor Author

@glebec glebec Sep 30, 2018

Choose a reason for hiding this comment

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

I'm curious why this PR's changes invalidated the former test and mean that the rels had to be taken out. I personally don't have any stake in those rels existing, just doing some due diligence.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nevermind – I was looking at only 81d998e's changes. I failed to notice that those changes undid some earlier changes in this same PR.

@@ -44,7 +44,7 @@
end

it "converts body_markdown to proper processed_html" do
expect(comment.processed_html.include?('<h1 id="hello-hy-hey-hey">')).to eq(true)
expect(comment.processed_html.include?('<h1 id="hello">')).to eq(true)
Copy link
Contributor Author

@glebec glebec Sep 30, 2018

Choose a reason for hiding this comment

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

Out of curiosity, why do ids behave differently now? If this test didn't make sense before, why did it previously pass? Is there any observable behavior change that might break earlier content?

Just doing due diligence, not saying how it ought to be.

Copy link
Contributor

Choose a reason for hiding this comment

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

The original test tested against <h1>, which would fail if the markdown text was

# hello

hy hey hey

This change just includes the anchor id in an otherwise unrelated test.

@glebec
Copy link
Contributor Author

glebec commented Sep 30, 2018

@bennypowers is this ready to have the WIP prefix removed (i.e. ready for this to be reviewed again)?

@bennypowers
Copy link
Contributor

@glebec yeah afaict it's ready to roll

@glebec glebec changed the title WIP feat(markdown): add ids to headers feat(markdown): add ids to headers Oct 1, 2018
@bennypowers
Copy link
Contributor

@Zhao-Andy interesting points for comparison: testing for emoji in headers.

@Zhao-Andy
Copy link
Contributor

Hey @glebec, finally getting around to this now; sorry about that. Mind if I push up to your branch directly? Seems to be the easiest way to keep the history clean.

@glebec
Copy link
Contributor Author

glebec commented Oct 11, 2018

I'm fine with that, will add you as a collaborator shortly. EDIT: ok, you should have received an invite to collab at https://github.com/glebec/dev.to.

@Zhao-Andy
Copy link
Contributor

Zhao-Andy commented Oct 11, 2018

Just made a push. I made a couple of changes:

  • I'd rather use an a tag with the name attribute instead of the built in with_toc_data option even though it's more verbose. This keeps us safe from people using headers with ids that match ours, and prevents them from using IDs that we don't want. I'm not sure how big of a security issue is, but seeing as GitHub uses a tags with names instead of headers with ids, it's probably safe to follow suit.
  • I added some styling that should make the headers properly jump to the header, as opposed to the paragraph. This happens because we have a sticky navbar on the top, and the native HTML jumps to the top of the header (or a tag in this case).

In the future, we might add an icon to the left side of the headers much like GitHub does in READMEs.

I tried that in my previous PR but it was such a pain to get the styling correct for both articles and comments. Maybe in the future though :)

@bennypowers
Copy link
Contributor

i'd suggest that emoji get stripped out of the anchor name rather than converted to unicode address

@Zhao-Andy Zhao-Andy force-pushed the feat/markdown-anchors branch from 29470b2 to 6cecb4a Compare October 12, 2018 14:17
@Zhao-Andy Zhao-Andy force-pushed the feat/markdown-anchors branch from 1f09ca1 to 5354261 Compare October 12, 2018 19:01
</h1>

<h1>
<a name="%F0%9F%98%8E-emoji-heading" href="#%F0%9F%98%8E-emoji-heading" class="anchor" id="%F0%9F%98%8E-emoji-heading">
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure why this looks like this file, but it works fine in development.

@Zhao-Andy
Copy link
Contributor

Here it is in action:
example-of-header-jumps

@bennypowers
Copy link
Contributor

Haven't reviewed the code but for the third example it should probably strip the trailing -

@bennypowers
Copy link
Contributor

👋

@Zhao-Andy
Copy link
Contributor

Hey @bennypowers, thanks for the bump. We'll get to this soon. 🙈

@pr-triage pr-triage bot added the PR: unreviewed bot applied label for PR's with no review label Nov 20, 2018
@pr-triage pr-triage bot added PR: reviewed-approved bot applied label for PR's where reviewer approves changes and removed PR: unreviewed bot applied label for PR's with no review labels Nov 20, 2018
@benhalpern benhalpern merged commit 1e2749b into forem:master Nov 20, 2018
@pr-triage pr-triage bot added PR: merged bot applied label for PR's that are merged and removed PR: reviewed-approved bot applied label for PR's where reviewer approves changes labels Nov 20, 2018
@glebec glebec deleted the feat/markdown-anchors branch November 21, 2018 03:50
@glebec glebec restored the feat/markdown-anchors branch November 21, 2018 03:50
@glebec glebec deleted the feat/markdown-anchors branch November 21, 2018 03:50
@glebec glebec restored the feat/markdown-anchors branch November 21, 2018 03:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: merged bot applied label for PR's that are merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

header tags should autogenerate anchor tags for Table of Contents usage
5 participants