-
Notifications
You must be signed in to change notification settings - Fork 528
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
Better Footnote title attributes? #387
Comments
How do you propose to handle footnotes like this one1. Footnotes
|
A very reasonable question! To me, it comes down to user needs. I want a tool-tip so I can see whether the footnote says "(Smith, 1996)" or "A funny story about that, back in 1996 I saw ...". That way, I can decide if I want to visit the footnote. Links are handled well, I think. The HTML spec for So I would use What do you think to that idea? |
I think truncating it if too long makes this idea reasonable. It should probably truncate at the first end of paragraph Sorry if this seems a lot of work. Maybe it's not all needed. I'm not sure if this tooltip should be the default, but at least it should be possible to enable or disable it. |
Things like Similarly, stopping at specific tag or replacing So, I guess the options are:
I'm lazy, so happy to contribute a patch for (1). But if you're looking for something more comprehensive, I'll have to bow out. |
Personally, I think we could cheat a bit more. Something like this:
There's some situations where the |
Here's a slightly less regex-y way of doing things using PHP's $doc = new DOMDocument();
$doc->loadHTML( mb_convert_encoding("<p>fugiat <em>consequuntur</em>. 蘵 <h2>Dolores deleniti</h2> <a href='/home'>quia voluptas</a> unde dolor <img alt='esse perspiciatis voluptas' src='img.jpg' />. <p>Autqui 🇬🇧<strong> corporisrerumfuga</strong> quo a. Fuga voluptatem rerum autem. Beatae dolorem et porro ut culpa. Est ut deserunt in quod non omnis suscipit veritatis.</p>", 'HTML', 'UTF-8' ));
$xp = new DOMXPath( $doc );
// Replace each element with the text inside it
foreach ( $xp->query('//*/text()') as $node ) {
$p_text = $doc->createTextNode( $node->wholeText . "\n");
$node->parentNode->replaceChild( $p_text, $node );
}
// Replace each <img> with its alt text
foreach ( $xp->query('//img') as $node ) {
$alt_text = $doc->createTextNode($node->getAttribute('alt') . "\n");
$node->parentNode->replaceChild($alt_text, $node);
}
// Replace each <br> with a newline
foreach ( $xp->query('//br') as $node ) {
$newline = $doc->createTextNode( "\n" );
$node->parentNode->replaceChild( $newline, $node );
}
// Get a plaintext representation
$title_text = html_entity_decode( strip_tags( $doc->saveHTML() ) );
// Trim any whitespace
$title_text = trim( $title_text ); The only problem is splitting the text. Not all multibyte languages (like Chinese) have word boundaries like |
I suppose a solution could be to have two limits: 100 characters + characters until word break, but if no word break is found between the 100th and 120th character you simply cut at 100 (or any other arbitrary length).
I think the |
I couldn't get that regex to work - and I'm always a little paranoid about how readable and maintainable they are. This should accomplish the same thing: // Split by space
$parts = explode( " ", $title_text );
// Add each part to a new string until it is 100 characters long
$title = "";
foreach ( $parts as $part) {
// Always add the first part
if ( mb_strlen( $title ) == 0 ) {
$title .= $part . " ";
// If the first part is a very long string, reduce it to 100 characters
if ( mb_strlen( $title ) > 100 ) {
$title = mb_substr( $title, 0, 100 );
break;
}
} else if ( ( mb_strlen( $title ) + mb_strlen( $part ) ) < 100 ) {
// Add the next whole word which doesn't take the total length over 100 characters
$title .= $part . " ";
} else {
break;
}
}
// Trim any whitespace
$title = trim( $title );
// If it has been truncated, add an ellipsis
if ( mb_strlen( $title ) < ( mb_strlen( $title_text ) ) ) {
$title .= "…";
} I've tested this with several long strings and it seems to work. I've also made some encoding changes to the above function so it copes better with multibyte characters. Let me know if you'd like this as a proper PR. |
One last bug :-)
php-markdown/Michelf/MarkdownExtra.php Line 408 in eb176f1
So, for example, a footnote which is a table is returned as Is there any way to unhash |
|
I ended up using |
Footnote links' titles can only be set once by the library. This change would make the attribute display the content of the footnote.
How
At the moment, if a user hovers over a footnote, the
title
attribute displays a tool tip.For example, if
php-markdown/Michelf/MarkdownExtra.php
Line 28 in eb176f1
is set to "Read the footnote." then all footnotes have this pop-up:
I think it would be nice if it showed users the full text of the footnote. For example:
This is controlled by :
php-markdown/Michelf/MarkdownExtra.php
Line 1764 in eb176f1
The change is relatively straightforward:
That takes the text of the footnote, sanitises it, and adds it as the title element.
Would you be interested in a PR for this?
(I have also raised this with WordPress's Jetpack which is running an older version of your library - Automattic/jetpack#27387)
The text was updated successfully, but these errors were encountered: