Skip to content

Commit

Permalink
Underscore RegEx hopefully good now
Browse files Browse the repository at this point in the history
  • Loading branch information
Moonbase59 committed Apr 2, 2024
1 parent 13a216a commit 2df7593
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
8 changes: 3 additions & 5 deletions gh-toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,16 @@ function tocIt(inputMD, minHeading, maxHeading, ignoreLinex)

headingLevel -= minHeading;

//var headingAnchor = headingTitle.toLowerCase().replace(/[^_\d\w\s-]/gu, "").replace(/\*(?=.*)/gu, "").replace(/ /gu, "-");
// make everything (Unicode-aware) lower case
var headingAnchor = headingTitle.toLowerCase();
// remove everything that is NOT a (Unicode) Letter, (Unicode) Number decimal,
// (Unicode) Number letter, white space, underscore or hyphen
headingAnchor = headingAnchor.replace(/[^\p{L}\p{Nd}\p{Nl}\s_-]/gu, "");
// remove sequences of *
headingAnchor = headingAnchor.replace(/\*(?=.*)/gu, "");
// TODO: Find a good way to keep underscores, except those for Markdown italics
// Right now, we’re just keeping underscores

// replace remaining blanks with '-'
// Try to keep underscores, except those for Markdown italics
headingAnchor = headingAnchor.replace(/(\s*)_+([^\s_].+[^\s_])_+(\s*)/gu, "$1$2$3");
// Now replace remaining blanks with '-'
headingAnchor = headingAnchor.replace(/ /gu, "-");

if(headingAnchor in anchorTracker)
Expand Down
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@
</head>
<body>
<h1>gh-toc</h1>
<p>a simple web page to quickly create table of content markdown for GitHub markdown files</p>
<p>A simple web page to quickly create table of content markdown for GitHub markdown files.</p>
<p><a href="https://github.com/moonbase59/gh-toc">https://github.com/moonbase59/gh-toc</a>
<h2>Overview</h2>
<blockquote>I could never find an easy to use, online table-of-content maker for GitHub markdown files. There were many solutions that required downloading or installing something on my computer. For such a trivial task I felt that was unnecessary.<br><em>(Words from the <a href="https://imthenachoman.github.io/nGitHubTOC/">original creator</a>)</em></blockquote>
<p>I fully agree. This is just a fork for my use, with no Google Analytics and some bugfixes.</p>
<p>This page just uses JavaScript to extract headings from a markdown file and return table-of-content markdown.</p>
<p>I did lots of testing and rewrote the RegExes to be as compatbile with GitHub as possible. So far it works great, even for difficult cases.</p>
<h2>The Magic</h2>
<p>
<table>
Expand Down
59 changes: 47 additions & 12 deletions testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,58 @@ These are some testing samples for [gh-toc](https://moonbase59.github.io/gh-toc/

Let’s check if these coincide with whatever GitHub Markdown comes up with!

## Foreign characters: äöüßÄÖÜẞ áéíóú ÁÉÍÓÚ àèìòù ñÑõÕ çÇ øØ
## The generated ToC (copy & pasted)

<!-- paste begin -->
- [gh-toc](#gh-toc)
- [The generated ToC (copy & pasted)](#the-generated-toc-copy--pasted)
- [Foreign characters](#foreign-characters)
- [Accents: äöüßÄÖÜẞ áéíóú ÁÉÍÓÚ àèìòù ñÑõÕ çÇ øØ](#accents-äöüßäöüß-áéíóú-áéíóú-àèìòù-ññõõ-çç-øø)
- [Russian: Какой-то русский текст…](#russian-какой-то-русский-текст)
- [Numbers: 0123 ⑴⑵⑶ ⅰ ⅱ ⅲ Ⅰ Ⅱ Ⅲ ²³ ¼½](#numbers-0123--ⅰ-ⅱ-ⅲ-ⅰ-ⅱ-ⅲ--)
- [Underscore special cases](#underscore-special-cases)
- [Some `code_with_underscores += a*b`](#some-codewithunderscores--ab)
- [More `code_bars`](#more-code_bars)
- [Just __ some underlines, or __dunders__](#just-__-some-underlines-or-dunders)
- [___](#___)
- [__](#__)
- [__dunder__](#dunder)
- [_emphasis_](#emphasis)
- [_ emph _](#_-emph-_)
- [_in a *sentence*_](#in-a-sentence)
- [_in_word_parsing_](#in_word_parsing)
- [Markdown](#markdown)
- [Some _emphasized_ text](#some-emphasized-text)
- [Some **bold** text](#some-bold-text)
- [Some ~~strikethrough~ text](#some-strikethrough-text)
- [More character codes](#more-character-codes)
- [Apostrophes and such: "a" 'b' “c” ‘d’ »e« ›f‹ „g“ ‚h‘](#apostrophes-and-such-a-b-c-d-e-f-g-h)
- [(Parentheses), [brackets] and {curlies}](#parentheses-brackets-and-curlies)
- [Arrows: ←↓↑→ ↕⇵ ⏎](#arrows---)
- [Symbols: ☯ ☎ 🂡](#symbols---)
- [Currency: $ € £ ₽ ¤](#currency-----)
- [Other: ℗©™](#other-)
<!-- paste end -->

## Foreign characters
### Accents: äöüßÄÖÜẞ áéíóú ÁÉÍÓÚ àèìòù ñÑõÕ çÇ øØ
### Russian: Какой-то русский текст…

## Numbers: 0123 ⑴⑵⑶ ⅰ ⅱ ⅲ Ⅰ Ⅱ Ⅲ ²³ ¼½

## Specials
## Underscore special cases
### Some `code_with_underscores += a*b`
### More `code_bars`
### Just __ some underlines, or __dunders__
### ___
### __
### __dunder__
### _emphasis_
### _ emph _
### _in a *sentence*_
### _in_word_parsing_

### Underscore stuff
#### Just __ some underlines, or __dunders__
#### ___
#### __
#### __dunder__
#### _emphasis_
#### _ emph _
#### _in a *sentence*_
#### _in_word_parsing_

## Markdown
### Some _emphasized_ text
### Some **bold** text
### Some ~~strikethrough~ text
Expand Down

0 comments on commit 2df7593

Please sign in to comment.