Comments (8)
You need to enable the Table of Contents extension.
from markdown.
I can see how it could work, but my md file already has the TOC in it, and it works in all the places I tried, meaning that the id-s are generated on the h2 tags, it only doesn't work when I use this markup python. So I hope there is a better solution that doesn't mean I have to delete the TOC from the md file, because then I'm not sure if it'll work in the other places.
from markdown.
If you read through the documentation, the extension will only generate the TOC if you provide[TOC]
in the file. If [TOC]
conflicts with anything you are doing, you can change that marker with the marker
parameter.
So, if all you want are anchors to be generated, enable toc
without injecting the [TOC]
marker (or whatever you choose as the marker) in your file.
from markdown.
If I add [TOC], then it doesn't work anywhere else. If I keep it as is it works everywhere but here. Is there a way to keep the manually added links and generate the ids for the anchors as it is done everywhere else?
from markdown.
If I add [TOC], then it doesn't work anywhere else. If I keep it as is it works everywhere but here. Is there a way to keep the manually added links and generate the ids for the anchors as it is done everywhere else?
I don't understand your question, but if you want anchors, this is all that is required:
import markdown
MD = """
# ANT+ HRM Heart Rate Monitor ❤️ - FAQ - Frequently Asked Questions
## Table of Contents
- [I installed ANT+ HRM to my watch but I don't see it anywhere](#i-installed-ant-hrm-to-my-watch-but-i-dont-see-it-anywhere)
## I installed ANT+ HRM to my watch but I don't see it anywhere
See: How can I add ANT+ HRM to my Run data screen?
"""
extensions = ['toc']
extension_configs = {}
print(markdown.markdown(MD, extensions=extensions, extension_configs=extension_configs))
<h1 id="ant-hrm-heart-rate-monitor-faq-frequently-asked-questions">ANT+ HRM Heart Rate Monitor ❤️ - FAQ - Frequently Asked Questions</h1>
<h2 id="table-of-contents">Table of Contents</h2>
<ul>
<li><a href="#i-installed-ant-hrm-to-my-watch-but-i-dont-see-it-anywhere">I installed ANT+ HRM to my watch but I don't see it anywhere</a></li>
</ul>
<h2 id="i-installed-ant-hrm-to-my-watch-but-i-dont-see-it-anywhere">I installed ANT+ HRM to my watch but I don't see it anywhere</h2>
<p>See: How can I add ANT+ HRM to my Run data screen?</p>
Please be more specific (maybe with an example?) to maybe illustrate exactly what the issue is.
from markdown.
As a reminder, adding anchors to heading sis NOT part of Markdown's syntax. We follow the original syntax rules, which make no mention of anchors. Even GitHub's own spec (an extended CommonMark spec) does not include any provision for anchors on headings. In fact, GitHub documents there markup process in github/markup. Note that while markdown is converted to HTML in step 1, it is not until step 4 that anchors are added. GitHub does not include adding anchors as part of their Markdown-to-HTML conversion but as a later postprocessor which is run against all markup languages consistently. The point is that it is not correct to expect a Markdown parser to handle this out-of-the-box.
As @facelessuser pointed our, we do provide an extension which does auto-generate anchors for headings. You can chose to use the TOC generated by the extension or use your own. However, if you chose to use your own, you will need to be sure to use the format for anchors defined by the extension. Alternatively, you could use the attr_list extension to define your own anchors in whatever format you want.
If you are still having issues, please provide a clear reproducible example. Please provide the input, the options passed to Markdown, and the output received as well as the expected output.
from markdown.
OK, that helped. Until now I used: python3 -m markdown TEST.md > TEST.html
I created now: md2html.py:
#!/usr/bin/env python3
import sys
import markdown
extensions = ['toc']
extension_configs = {}
def main(argv):
with open(argv[0], 'r') as md_file:
md = md_file.read()
print(markdown.markdown(md, extensions=extensions, extension_configs=extension_configs))
if __name__ == '__main__':
main(sys.argv[1:])
from markdown.
You can use markdown
from the command line, you just have to configure the extensions:
Usage: __main__.py [options] [INPUTFILE]
(STDIN is assumed if no INPUTFILE is given)
A Python implementation of John Gruber's Markdown. https://Python-
Markdown.github.io/
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-f OUTPUT_FILE, --file=OUTPUT_FILE
Write output to OUTPUT_FILE. Defaults to STDOUT.
-e ENCODING, --encoding=ENCODING
Encoding for input and output files.
-o OUTPUT_FORMAT, --output_format=OUTPUT_FORMAT
Use output format 'xhtml' (default) or 'html'.
-n, --no_lazy_ol Observe number of first item of ordered lists.
-x EXTENSION, --extension=EXTENSION
Load extension EXTENSION.
-c CONFIG_FILE, --extension_configs=CONFIG_FILE
Read extension configurations from CONFIG_FILE.
CONFIG_FILE must be of JSON or YAML format. YAML
format requires that a python YAML library be
installed. The parsed JSON or YAML must result in a
python dictionary which would be accepted by the
'extension_configs' keyword on the markdown.Markdown
class. The extensions must also be loaded with the
`--extension` option.
-q, --quiet Suppress all warnings.
-v, --verbose Print all warnings.
--noisy Print debug messages.
I use the script I posted simply because I just copy and paste people's example into it so I can replicate their issues and provide examples. Either way, glad you have a working solution.
from markdown.
Related Issues (20)
- Strange and inconsistent parsing of lists with headers and multiple lines HOT 6
- The title from `toc_tokens` ignores the `smarty` extension HOT 3
- BlockProcessor output wrapped in p tag HOT 9
- Trying to migrate from `markdown2`, lists without blank lines not working HOT 3
- "Unterminated character set" exception when using extra extension HOT 6
- <table> improperly wrapped by <p> when inside a list HOT 1
- Add Support for Tab Customization in Code Blocks HOT 3
- List not rendering correctly when preceded by word HOT 1
- add a buildin Mermaid extention HOT 1
- No table support? HOT 8
- GFM alerts HOT 2
- TOC extension: ignore headers above [TOC] marker HOT 2
- Fenced code blocks with newlines not parsed into html <code> blocks HOT 3
- convert \u2028 to <br>? HOT 2
- `abbr` extension breaks title attribute added using `attr_list` HOT 1
- Separate extensions from core
- A line ending that is preceded by a backslash is not parsed as a hard line break HOT 3
- Abbreviation processing order alternative HOT 9
- 'markdown_extras' is not a registered tag library. Must be one of: HOT 7
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.