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

Heading auto identifier #175

Open
wants to merge 35 commits into
base: master
Choose a base branch
from

Conversation

chowette
Copy link

@chowette chowette commented Apr 12, 2022

This PR implement an extension when generating heading. This make possible to link directly to heading.
Github, Pandoc and other Markdown libs provide this feature.

Because this is an extension, there is a flag and a command line switch to enable it.
MD_FLAG_HEADINGAUTOID and corresponding --fheading-auto-id are added

This PR is a work in progress and the tasks remaining are:

  • identifier generation in HTML

  • identifier transformation like github

    • space changed to -
    • uppercase to lower case
    • Unicode upper case to lower case ΑΓΩ -> αγω
    • identical heading have ending number starting with -1
    • links and footnote are skipped
    • Emojis are treated as ponctuation, Unicode emoji are stripped
  • identifier transformation like Pandoc

    • space changed to -
    • leave _and ., but do not add additional - for surrounding space
    • uppercase to lower case
    • Unicode upper case to lower case ΑΓΩ -> αγω
    • leading chapter numbers are skipped
    • identical heading have ending number starting with -1
    • links and footnote are skipped
    • empty identifier are replaced by section
  • add a Table of Content

    • store heading title and level
    • numbering (eg: 1.2.3)
    • tag for TOC placement in document
      • [[_TOC_]] like RedCarpet Markdown
      • {:toc} like Kramdown Markdown
      • [toc] like Typora Markdown

Add TOC option to the parser parameter struct for md_parse() and md_html()

  • Increase the abi_version to 1
  • add --table-of-content[=MARK] and --toc[=MARK] option to md2html
  • add --toc-depth=x to limit TOC levels

This imply scanning and transforming the heading text.
As the heading text is transformed, we need to stor it somewhere.
Instead of doing almost one malloc for each heading, allocate a large
buffer and store all indentifier inside, and using index.

We also remember the heading in the header Block to later retrieve it.
Do not hold pointer to reallocated memory. Realloc can copy to a
new location and our pointers are now invalid.
memorize the offset in the buffer instead, this remain valid even after
reallochas relocated the buffer
the block line(s) is always trimed when build
like `ΑΓΩ` is changed to `αγω`
It works but need more work because
- bad O(n²) algorithm to be replaced by a hash[Map|Set]
- use snprintf for simple int_to_str implementation
@codecov
Copy link

codecov bot commented Apr 23, 2022

Codecov Report

Merging #175 (a41ab75) into master (e9ff661) will increase coverage by 0.04%.
The diff coverage is 92.70%.

@@            Coverage Diff             @@
##           master     #175      +/-   ##
==========================================
+ Coverage   94.32%   94.36%   +0.04%     
==========================================
  Files           3        3              
  Lines        3100     3496     +396     
==========================================
+ Hits         2924     3299     +375     
- Misses        176      197      +21     
Impacted Files Coverage Δ
src/md4c.c 94.27% <92.53%> (+0.04%) ⬆️
src/md4c-html.c 95.36% <100.00%> (+0.16%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

 ISUNICODEPUNCT_() use a codepoint not an offset. use ISPUNCT_()
some indentation cleaning
Use a pointer and a size instead of begining and ending index.
We need a pointer because we want to store the heading
identifier as destination, but the identifier is not part of the
initial ctx->txt buffer. This is done like the `title`
We also cascade change the `MD_LINK_ATTR` struct and supporting functions
so we can reuse it for heading
- depth for toc output
- Increase the abi_version to 1
- add --table-of-content option to md2html
- add --toc-depth=x to limit TOC levels
--table-of -content option has a parameter to set the mark
--toc is a shorthand for the --table-of-content option

wrong TOC depth is now an error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant