- ๐ฑ Iโm currently learning everyting ๐คฃ
- ๐ฏ Iโm looking to collaborate with other content creators
benjaminhoegh / parsedowntoc Goto Github PK
View Code? Open in Web Editor NEWTable of content for Parsedown
License: MIT License
Table of content for Parsedown
License: MIT License
I use your class with the last release of ParsedownExtra and Parsedown.
When I try to generate a TOC, I've these errrors :
PHP Warning: Illegal string offset 'argument' in /my_project//vendor/ParsedownToc.php on line 47
PHP Notice: Undefined index: attributes in /my_project//vendor/ParsedownToc.php on line 58
PHP Warning: Illegal string offset 'argument' in /my_project//vendor/ParsedownToc.php on line 60
PHP Warning: Illegal string offset 'argument' in /my_project/vendor/ParsedownToc.php on line 60
For information, theses lines are impacted :
47: if (preg_match('/[ #]*{('.$this->regexAttribute.'+)}[ ]*$/', $Block['element']['handler']['argument'], $matches, PREG_OFFSET_CAPTURE)) {
58: $link = "#".$Block['element']['attributes']['id'];
60: $Block['element']['handler']['argument'] = $Block['element']['handler']['argument']."<a class='heading-link' href='{$link}'> <i class='fas fa-link'></i></a>";
I don't autided too Pardown source code to known how to solve this problem, can you solve ?
Thanks, have a nice day.
If we use ParsedownExtra insted of Parsedown we have this error :
PHP Fatal error: Uncaught Exception: Parsedown-toc requires a later version of Parsedown in /my_project/vendor/ParsedownToc.php:10
I've patch with this :
// ...
public function __construct()
{
$vMin = (get_parent_class()=='ParsedownExtra') ? '0.7.0' : '0.7.1';
if (version_compare(parent::version, $vMin) < 0) {
// ....
Have a nice day.
I found a cleaner solution to dynamically load Parsedown or ParsedownExtra as parent: class_alias
.
Here is my implementation in action:
https://codeberg.org/raffaelj/parsedown-tasks/src/branch/main/ParsedownTasks.php#L6-L14
This removes one problem of multiple classes named DynamicParent
in the global scope, when trying to mix multiple Parsedown addons, that use that same technique.
If you want to implement it, make sure to check for is_callable('parent::__construct')
in your constructor to fix the missing constructor in Parsedown
.
And while I'm here - instead of opening another issue - it's better to autoload with psr-0
instead of with files
. The difference is, that autoloading with files
always includes the file, when require(__DIR__.'/vendor/autoload.php');
is called. Autoloading with psr-0
only includes the file when the class is in use, e. g. $test = new Test();
. This is a huge performance improvement when dealing with many classes.
Thanks again for your work and have a nice day,
Raffael
Describe the bug
contentsList does not output any text
$Parsedown->contentsList();
To Reproduce
$Parsedown = new ParsedownToC();
$fh = file_get_contents('/docs.md');
$toc = $Parsedown->contentsList();
echo $toc;
Expected behavior
output a HTML list of headings
Fix
protected function blockHeader($Line)
{
// set this here to prevent $text not declared error/warning
$text = '';
// Use parent blockHeader method to process the $Line to $Block
$Block = DynamicParent::blockHeader($Line);
if (!empty($Block)) {
// Get the text of the heading
// if (isset($Block['element']['text'])) {
// $text = $Block['element']['text'];
// }
if(isset($Block['element']['handler']['argument'])){
$text = $Block['element']['handler']['argument'];
}
Thanks for your impressive work. I really appreciate it. I fiddled a bit in the last days with your ParsedownExtended repo (outdated?), with this repo and with combining it with the ParsedownCheckbox extension. They both work well together.
Is your feature request related to a problem? Please describe.
I have md files as content and a template as wrapper. The wrapper already contains some ids, that can be duplicated via your extension. A simplified example:
template:
<body>
<span id="top">
<main><!-- my rendered markdown --></main>
<a href="#top">
</body>
content/markdown:
# Docs
If you create a child theme, make sure that the parent theme already uses a handful of ids.
## Reserved ids
### top
jump to top
### privacy-notice
js target to display the privacy/cookie popup
After rendering the md file, if have duplicated ids in my markup.
Describe the solution you'd like
$parser = new \ParsedownToC();
$parser->options['blacklist'] = ['top', 'nav', 'privacy-notice']; // strings or regexes
echo $parser->text($md);
The rendered text should increase the number suffix, like it would with duplicated headings by default.
<h3 id="top-1">top</h4>
<h3 id="privacy-notice-1">privacy notice</h4>
Describe alternatives you've considered
I used the replacements
option and it seemed to work well...
$parser->options['replacements'] = [
'/^nav$/' => 'nav-1',
'/^top$/' => 'top-1',
'/^main$/' => 'main-1',
'/^privacy-notice/' => 'id_privacy-notice',
];
But the replacements fire too early. If my markdown contains # Privacy notice
, the regex doesn't match. I could write a regex, that matches all possibilities after all the other conversions happened inside createAnchorID()
, but a blacklist of ids would really help to catch already taken ids.
I don't want to write such things: /^(?:-|_)*(?:privacy)(?:\s|-)(?:notice)/i
And I really don't want to write regexes, that match the final string after the char map conversion.
The new filter should fire after the lowercase
filter and it should consider edge cases while increasing the number suffix if e. g. top-2
is blacklisted and 3 headings ## top
are present.
edit: fixed typos
As mentioned in #15, Parsedown
has no __construct()
method.
To Reproduce
mkdir test;
cd test;
composer require benjaminhoegh/parsedown-toc
php -r 'require __DIR__."/vendor/autoload.php";new ParsedownToc();'
PHP Fatal error: Uncaught Error: Cannot call constructor in /path/to/test/vendor/benjaminhoegh/parsedown-toc/src/ParsedownToc.php:45
Stack trace:
#0 Command line code(1): ParsedownToc->__construct()
#1 {main}
thrown in /path/to/test/vendor/benjaminhoegh/parsedown-toc/src/ParsedownToc.php on line 45
Solution
Change
parent::__construct();
to
if (is_callable('parent::__construct')) {
parent::__construct();
}
in https://github.com/BenjaminHoegh/ParsedownToc/blob/master/src/ParsedownToc.php#L45
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.