Skip to content

Heading auto identifier#175

Open
chowette wants to merge 35 commits into
mity:masterfrom
chowette:heading-auto-identifier
Open

Heading auto identifier#175
chowette wants to merge 35 commits into
mity:masterfrom
chowette:heading-auto-identifier

Conversation

@chowette
Copy link
Copy Markdown

@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

chowette added 12 commits March 30, 2022 18:37
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
Copy Markdown

codecov Bot commented Apr 23, 2022

Codecov Report

❌ Patch coverage is 92.70588% with 31 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.36%. Comparing base (e9ff661) to head (a41ab75).
⚠️ Report is 162 commits behind head on master.

Files with missing lines Patch % Lines
src/md4c.c 92.53% 31 Missing ⚠️
Additional details and impacted files
@@            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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

 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