Coder Social home page Coder Social logo

parsedowntoc's Introduction

Hi there ๐Ÿ‘‹ I'm Benjamin

  • ๐ŸŒฑ Iโ€™m currently learning everyting ๐Ÿคฃ
  • ๐Ÿ‘ฏ Iโ€™m looking to collaborate with other content creators

benjaminhoegh

Connect with me:

benjaminhoegh benjamin_hoegh

Languages and Tools:

css3 html5 java javascript mysql php python sass swift

Support:

BenjaminHoegh



parsedowntoc's People

Contributors

arnaudligny avatar benjaminhoegh avatar jifish avatar pjbakker avatar raffaelj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

parsedowntoc's Issues

No "argument" in $Block

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.

Error of version with ParsedownExtra

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.

use `class_alias` instead of `DynamicParent` class and autoload with `psr-0`

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

Empy list

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'];
      }

blacklist ids

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

1.5 is not compatible with Parsedown (only works with ParsedownExtra)

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.