Comments (10)
Hi. A PR with a fix would be very welcome. Although I would argue that no styling should be transfered to the TOC. The formatting should rather be stripped away.
from markdown-it-table-of-contents.
format
hook can be used to render this markup as html. But this creates additional problem how to get only text nodes from this rendered html...
from markdown-it-table-of-contents.
Workaround:
const removeMarkdown = require('remove-markdown');
const toc = require('markdown-it-table-of-contents');
const md = require('markdown-it')();
md.use(toc, {
format: removeMarkdown,
});
const src = `
[[toc]]
# *Title*
## Subtitle
`;
md.render(src, {});
from markdown-it-table-of-contents.
I guess we can close this. Workaround/Solution mentioned above work for most usecases.
from markdown-it-table-of-contents.
👍
from markdown-it-table-of-contents.
Hi. Thanks for making this plugin! It's been really helpful.
I'd like to re-raise this issue. By default, I think this library should either:
- Strip out markdown (e.g.
My **bold** title
becomesMy bold title
). - Process the markdown (e.g.
My **bold** title
becomesMy <strong>bold</strong> title
)
The current default behavior – rendering unprocessed markdown – is probably not what anyone wants or expects. I understand you can achieve either of these outcomes using a format
function. But, ideally, users of this extension could avoid that work if a smarter default were in place.
Of the two options I've suggested, I think it would be better and easier to process the markdown. (Stripping could be tricky given the full range of possibilities, especially if you consider rarer options like superscript, subscript, etc.).
I'm using this lib to render ToCs for scientific articles. In my (admittedly complex) use case, headers may include a variety of formatting (e.g. italics, superscripts, even equations). And this formatting is often integral to header's legibility / meaning (i.e. it's not just a matter of style).
How to implement / Need help?
Looking at your code, I think the md
object may have access to the underlying rendering functions. For example, I wrote a markdown-it extension which did something like:
this.env.md.renderInline("Some text which _may have formatting_")
And this allowed me to process markdown text within my extension – which I think is what we want in this case as well.
I'm not 100% sure how this would work in your code, but I think it's possible (and probably pretty easy). If you need help, lemme know and could probably make a PR.
from markdown-it-table-of-contents.
Hi! Thanks for chipping in...
The current default behavior – rendering unprocessed markdown – is probably not what anyone wants or expects.
I agree.
I'm using this lib to render ToCs for scientific articles. In my (admittedly complex) use case, headers may include a variety of formatting (e.g. italics, superscripts, even equations). And this formatting is often integral to header's legibility / meaning (i.e. it's not just a matter of style).
I haven't considered this scenario. It makes sense.
Of the two options I've suggested, I think it would be better and easier to process the markdown.
Given your examples I see your point. However, I think it would be preferable if it could be an option like so: parseMarkdownInHeadings:
true
or false
where true
should be the default option.
Do you think it's somewhat plausible to implement stripping markdown? Haven't looked at it in any depth but I imagine it's possible by either hooking into the markdown-it parser or by stripping markdown in the raw text via regexp or similar (I've seen libs that do that)...
from markdown-it-table-of-contents.
Thanks for considering this change! I like the idea of adding a parseMarkdownInHeadings
option which defaults to true
.
As I said, I think processing the markdown should be easy (maybe even a one-liner). Stripping the markdown is a bit trickier.
Here's what I would suggest:
1. Use an existing library
Both remove-markdown and strip-markdown seem decent. That said, they do seem a little greedy and could result in striping non-markdown text in rare edge cases (e.g. if your heading includes a non-markdown _
or [
). But maybe that's okay?
2. Don't even try
Maybe, instead, give folks the option to render the raw markdown (i.e. parseMarkdownInHeadings: false
) and let them deal with the complexity as well as the potential edge cases by writing a custom format
function. For example, if I know my headers will only include *
, then a simple regex would suffice. Or, if I have more possibilities, then I could consider using something like remove-markdown within my format
function.
Most of all, I would avoid trying to write your own regex. Just my opinion but I think it could end up being a messy, half-solution. And, given a tricky problem, I'd rather avoid it altogether than apply a partial fix. But that's just my opinion.
from markdown-it-table-of-contents.
Ok, I hear you...
Then maybe we could just consider changing the default for the format
option to a function that returns parsed markdown. Sort of what you initially suggested 😄 ...
A PR with this change would be very welcome!
from markdown-it-table-of-contents.
@martinlissmyr Sorry I dropped the ball on this fix for several months. But, finally, I have a PR for you to review: #41
Thanks again for creating this extension!
from markdown-it-table-of-contents.
Related Issues (20)
- Library doesn't support AMD HOT 1
- Remove dependency on [email protected] which has a security vulnerability (ReDos) HOT 2
- Have a heading for the table of contents HOT 4
- Table of contents rendered incorrectly if no empty line is given before and after the [[toc]] HOT 1
- TOC regex pattern should be matched on the state.src only from the current position parsed HOT 2
- If the header is also a link, the href attribute in <a> will be wrong. HOT 2
- Recognize IDs added by markdown-it-attrs HOT 2
- Unable to pass options object into plugin. HOT 1
- TOC with diacritics not working in IE HOT 1
- Rule link_open HOT 2
- Conflicting with emojis HOT 2
- Support {{TOC}} pattern additionally to [[TOC]] HOT 4
- It would be nice to have a more flexible content generation for links in toc HOT 2
- Mistake in readme with anchor plugin
- Interested in maintaining package HOT 12
- Doesn't respect custom heading ids from markdown-it-attrs / no way to define custom ids
- Incomplete TOC when nesting order is unexpected
- support typora style toc HOT 2
- Add attributes to the TOC list HOT 6
- KaTeX Expressions aren't Included in Anchor Links
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from markdown-it-table-of-contents.