Skip to content
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

Is it posible to make it work with [toc] #2

Closed
BenjaminHoegh opened this issue Feb 15, 2019 · 3 comments
Closed

Is it posible to make it work with [toc] #2

BenjaminHoegh opened this issue Feb 15, 2019 · 3 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@BenjaminHoegh
Copy link

Is it posible to make it work with [toc] ???

@KEINOS
Copy link
Owner

KEINOS commented Feb 26, 2019

@BenjaminHoegh

Sadly, so far at this latest commit 4e9af97, NO. 😭

Though, I agree and like your idea. It would be nice if we can have a [toc] markdown tag which works like below:

  • Markdown
    [toc]
    
    ---
    
    # Head1
    Sample text of head 1.
    ## Head1-1
    Sample text of head 1-1.
  • Result
    <ul>
    <li><a href="#Head1">Head1</a>
    <ul>
    <li><a href="#Head1-1">Head1-1</a></li>
    </ul></li>
    </ul>
    <hr>
    <h1 id="Head1" name="Head1">Head1</h1>
    <p>Sample text of head 1.</p>
    <h2 id="Head1-1" name="Head1-1">Head1-1</h2>
    <p>Sample text of head 1-1.</p>

It might be a feasible idea if we can replace the [toc] tag to the value which contentsList() method was holding at some point. Maybe by overriding the text() method?

@KEINOS KEINOS added enhancement New feature or request question Further information is requested labels Feb 26, 2019
@KEINOS
Copy link
Owner

KEINOS commented Feb 26, 2019

A simple workaround with the current version would be:

<?php
include_once('Pasedown.php');
include_once('Extension.php');

$textMarkdown =<<<EOL
[toc]

---
# Head1
Sample text of head 1.
## Head1-1
Sample text of head 1-1.
# Head2
Sample text of head 2.
## Head2-1
Sample text of head2-1.
EOL;

$Parsedown = new Extension();
$body = $Parsedown->text($textMarkdown);
$ToC  = $Parsedown->contentsList();

// Replace `[toc]` to generated table of contents
$body = str_replace('[toc]', $ToC, $body);

echo $body . PHP_EOL;

KEINOS added a commit that referenced this issue Sep 21, 2019
KEINOS added a commit that referenced this issue Sep 23, 2019
@KEINOS
Copy link
Owner

KEINOS commented Sep 23, 2019

[toc] now available!

@BenjaminHoegh
Is it posible to make it work with [toc] ???

As of f027f1f, now it works with [toc] tag! Thanks for waiting.

  • The Markdown file:
[toc]

---

# One
Something about One

## Two
Something about Two

# One2
Something about One2

The PHP file. (It uses composer. Do composer require keinos/parsedown-toc before use)

  • NOTE: the main class' name's been changed from Extension() to ParsedownToC() since v1.0.0
require_once __DIR__ . '/vendor/autoload.php';

$Parsedown = new \ParsedownToC();
$text_markdown = file_get_contents('SAMPLE.md');
$html = $Parsedown->text($text_markdown);

echo $html . PHP_EOL;

The result

$ php main.php
<div id="toc"><ul>
<li><a href="#One">One</a><ul>
<li><a href="#Two">Two</a></li>
</ul>
</li>
<li><a href="#One2">One2</a></li>
</ul></div>
<hr />
<h1 id="One" name="One">One</h1>
<p>Something about One</p>
<h2 id="Two" name="Two">Two</h2>
<p>Something about Two</p>
<h1 id="One2" name="One2">One2</h1>
<p>Something about One2</p>

Closing the issue. But feel free to re-open the issue if it's not working!

@KEINOS KEINOS closed this as completed Sep 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants