question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sub headings not getting direct linkified

See original GitHub issue

I noticed these two <h2> tags aren’t being highlighted and turned into a direct link like they should be. This is happening on the Using CSS Variables page.

Screen Shot 2021-01-28 at 8 13 53 PM Screen Shot 2021-01-28 at 8 15 57 PM Screen Shot 2021-01-28 at 8 16 46 PM Screen Shot 2021-01-28 at 8 13 35 PM

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
peterbecommented, Jan 29, 2021

What’s happening here is that we have HTML looks like this:

<p>intro bla bla</p>

<h2>Basic usage</p>
<p>blabla</p>

<h2>First steps with custom properties</h2>
<div id="sample1">
  <p>blabla</p>
  <h2>Inheritance of custom properties</h2>
</div>

What the builder does is that it first breaks it up into sections by looping over the “root level” elements. The first and section section is:

<p>intro bla bla</p>

and

<h2>Basic usage</p>
<p>blabla</p>

But for the next section, yes it starts with a lovely <h2> but here’s more than one within

<h2>First steps with custom properties</h2>
<div id="sample1">
  <p>blabla</p>
  <h2>Inheritance of custom properties</h2>
</div>

So it dares not to extract split it by <h2>. In fact, to extract the title, it uses:

const h2s = $.find("h2");
if (h2s.length === 1) {
    id = h2s.attr("id");
    title = h2s.html();
    titleAsText = h2s.text();
    h2s.remove();
}

But what happens here on this page is that h2s.length === 2 so it can’t confidently proceed.

The solution is two-fold. First, we need to correct the HTML. That second <h2>Inheritance of custom properties</h2> should be moved and appear outside the <div id="sample1"> so it’s at the root level instead. Secondly, because there’s a lot of pages that are like this. We can do a little bit better. So we can change the id and title extraction to:

const h2s = $.find("h2");
if (h2s.length >= 1) {
    id = h2s.attr("id"); 
    title = h2s.html();
    titleAsText = h2s.text();
    h2s.eq(0).remove();  // only remove the first one
}

That, and log it as a flaw (not fixable) where a writer has to go in and move the <h2> to the root level.

1reaction
peterbecommented, Jan 29, 2021

So what’s probably happening is that some invalid HTML gets forgiven by cheerio and when serialized back to a HTML string, it adds that <div> lets see what’s going on.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MS Word - Built in Headings not working - Microsoft Community
I am having a problem with my MS word, I am currently trying to format one of my reports for varsity so that...
Read more >
headings not showing up in navigation pane in word - YouTube
Learn how to add title in the navigation bar which is very helpful in navigating to different parts of the word documents.
Read more >
Syntax Extensions - MyST-Parser
This extension requires that linkify-it-py is installed. Either directly; pip install linkify-it-py or via pip install myst-parser[linkify] .
Read more >
Configure Markup - Hugo
How to handle Markdown and other markup related configuration.
Read more >
Microsoft Word - Adding Headings to a Document
One of the key ways to make Microsoft Word documents accessible is to use Word's built-in heading styles to format the document.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found