Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Autogenerated Table of Contents for Github Markdown or Bear Notes
# Autogenerated Table of Contents for Github, GitLab Markdown or Bear Notes

> Generated by the script ;)
## Table of Contents
Expand Down Expand Up @@ -33,7 +33,7 @@ $ python3 setup.py install
## Usage
### Flags
* `-h` or `--header-priority` is the Limiting Header Priority/Strength (whether you limit your Table of Contents to h3s or h4s)
* `-t` or `--type` is the type of anchor you want to use (Github or Bear x-callback-url)
* `-t` or `--type` is the type of anchor you want to use (Github, Gitlab or Bear x-callback-url)
* `--no-write` is a flag on whether you want to disable the feature that automatically injects the ToC into your Markdown file or Bear Note
* `-toc` or `--table-of-contents-style` is the Markdown style for your Table of Contents header

Expand Down
16 changes: 12 additions & 4 deletions markdowntoc/markdowntoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
parser.add_argument('-h', '--header-priority', type=int, dest='header_priority', default=3,
help='(Default: 3) Maximum Header Priority/Strength to consider as Table of Contents')

parser.add_argument('-t', '--type', type=str.lower, dest='type', choices=['github', 'bear'], default='github',
parser.add_argument('-t', '--type', type=str.lower, dest='type', choices=['gitlab', 'github', 'bear'], default='github',
help='(Default: github) Github Anchors or Bear Anchors')

parser.add_argument('--no-write', dest='write', action='store_false',
Expand Down Expand Up @@ -170,6 +170,11 @@ def create_github_header_anchor(header_title):
"""
return '[{}](#{})'.format(header_title, header_title.strip().replace(' ', '-'))

def create_gitlab_header_anchor(header_title):
"""
Returns a Gitlab Markdown anchor to the header.
"""
return '[{}](#{})'.format(header_title, header_title.lower().strip().replace(' ', '-'))

def create_table_of_contents(header_priority_pairs, note_uuid=None):
"""
Expand All @@ -182,7 +187,10 @@ def create_table_of_contents(header_priority_pairs, note_uuid=None):

highest_priority = min(header_priority_pairs, key=lambda pair: pair[1])[1]
for header, priority in header_priority_pairs:
md_anchor = create_bear_header_anchor(header, note_uuid) if params['type'] == 'bear' else create_github_header_anchor(header)
md_anchor = create_bear_header_anchor(header, note_uuid) if params['type'] == 'bear' \
else create_github_header_anchor(header) if params['type'] == 'github' \
else create_gitlab_header_anchor(header)

bullet_list.append('\t' * (priority - highest_priority) + '* ' + md_anchor)

# Specifically for Bear add separator
Expand Down Expand Up @@ -227,7 +235,7 @@ def create_table_of_contents_bear():
return md_text_toc_pairs, uuids


def create_table_of_contents_github():
def create_table_of_contents_github_or_gitlab():
"""
Read from file and returns list of (Original Text, Table of Contents List).
"""
Expand Down Expand Up @@ -294,7 +302,7 @@ def main():
if (params['type'] == 'bear'):
md_text_toc_pairs, identifiers = create_table_of_contents_bear()
elif (params['type'] == 'github'):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want to add params['type'] == 'github' or params['type'] == 'gitlab' here?

md_text_toc_pairs, identifiers = create_table_of_contents_github()
md_text_toc_pairs, identifiers = create_table_of_contents_github_or_gitlab()

for i, (md_text, toc_lines) in enumerate(md_text_toc_pairs):
if (params['write']):
Expand Down