Skip to content

Commit

Permalink
Merge pull request #40 from didix21/36-table-of-contents-link-does-no…
Browse files Browse the repository at this point in the history
…t-work-

Fix #36: Delete special characters when creating toc links
  • Loading branch information
didix21 authored Jun 21, 2020
2 parents d461cbe + 5b821ea commit cbd8bb6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 25 deletions.
2 changes: 1 addition & 1 deletion mdutils/tools/Link.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def new_link(link, text=None, tooltip=None):

@staticmethod
def __md_link(link, text):
return '[' + text + '](' + link + ')'
return TextUtils.text_external_link(text, link)


if __name__ == "__main__":
Expand Down
4 changes: 3 additions & 1 deletion mdutils/tools/TableOfContents.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# This file is part of mdutils. https://github.com/didix21/mdutils
#
# MIT License: (C) 2020 Dídac Coll
import re


class TableOfContents:
Expand All @@ -30,7 +31,8 @@ def _loop_through(self, elements, tab='', depth=1):
else:
elements_to_string += self._loop_through(item, tab='\t')
else:
elements_to_string += '\n' + tab + '* [' + item + '](#' + item.lower().replace(' ', '-') + ')'
elements_to_string += '\n' + tab + '* [' + item + '](#' \
+ re.sub('[^a-z0-9_\-]', '', item.lower().replace(' ', '-')) + ')'

return elements_to_string

Expand Down
45 changes: 24 additions & 21 deletions tests/test_mdutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,29 @@

from pathlib import Path
import os
import re


class TestMdUtils(TestCase):

def setUp(self) -> None:
self.expected_list = "\n\n\n" \
"\n- Item 1\n" \
"- Item 2\n" \
"- Item 3\n" \
"- Item 4\n" \
" - Item 4.1\n" \
" - Item 4.2\n" \
" - Item 4.2.1\n" \
" - Item 4.2.2\n" \
" - Item 4.3\n" \
" - Item 4.3.1\n" \
"- Item 5\n"
"\n- Item 1\n" \
"- Item 2\n" \
"- Item 3\n" \
"- Item 4\n" \
" - Item 4.1\n" \
" - Item 4.2\n" \
" - Item 4.2.1\n" \
" - Item 4.2.2\n" \
" - Item 4.3\n" \
" - Item 4.3.1\n" \
"- Item 5\n"
self.complex_items = ["Item 1", "Item 2", "Item 3", "Item 4",
["Item 4.1", "Item 4.2",
["Item 4.2.1", "Item 4.2.2"],
"Item 4.3", ["Item 4.3.1"]],
"Item 5"]
["Item 4.1", "Item 4.2",
["Item 4.2.1", "Item 4.2.2"],
"Item 4.3", ["Item 4.3.1"]],
"Item 5"]

def tearDown(self):
md_file = Path('Test_file.md')
Expand Down Expand Up @@ -78,10 +79,10 @@ def test_new_table_of_contents(self):
# Testing Depth 1
table_of_contents_result = md_file.new_table_of_contents(table_title="Index", depth=1)
table_of_content_expected = table_of_content_title \
+ '\n* [' + list_headers[0] + '](#' + list_headers[0].lower().replace(' ', '-') \
+ ')' \
+ '\n* [' + list_headers[2] + '](#' + list_headers[2].lower().replace(' ', '-') \
+ ')\n'
+ '\n* [' + list_headers[0] + '](#' \
+ re.sub('[^a-z0-9_\-]', '', list_headers[0].lower().replace(' ', '-')) + ')' \
+ '\n* [' + list_headers[2] + '](#' \
+ re.sub('[^a-z0-9_\-]', '', list_headers[2].lower().replace(' ', '-')) + ')\n'
self.assertEqual(table_of_contents_result, table_of_content_expected)
# Testing created file
md_file.create_md_file()
Expand All @@ -107,10 +108,12 @@ def test_new_table_of_contents(self):
for x in range(len(list_headers)):
if x in (0, 2):
table_of_content_expected += '\n* [' + list_headers[x] + '](#' \
+ list_headers[x].lower().replace(' ', '-') + ')'
+ re.sub('[^a-z0-9_\-]', '', list_headers[x].lower().replace(' ', '-')) \
+ ')'
else:
table_of_content_expected += '\n\t* [' + list_headers[x] + '](#' \
+ list_headers[x].lower().replace(' ', '-') + ')'
+ re.sub('[^a-z0-9_\-]', '', list_headers[x].lower().replace(' ', '-')) \
+ ')'
table_of_content_expected += '\n'
self.assertEqual(table_of_contents_result, table_of_content_expected)

Expand Down
28 changes: 26 additions & 2 deletions tests/test_tools/test_table_of_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,36 @@ class TestTableOfContents(TestCase):
def test_create_table_of_contents(self):
array_of_contents = ['Results Tests', [], 'Test Details', ['Test 1', [], 'Test 2', [],
'Test 3', [], 'Test 4', []]]
result = '\n* [Results Tests](#results-tests)\n' \
expects = '\n* [Results Tests](#results-tests)\n' \
'* [Test Details](#test-details)\n\t' \
'* [Test 1](#test-1)\n\t' \
'* [Test 2](#test-2)\n\t' \
'* [Test 3](#test-3)\n\t' \
'* [Test 4](#test-4)\n'

table_of_contents = TableOfContents()
self.assertEqual(table_of_contents.create_table_of_contents(array_of_contents, depth=2), result)
self.assertEqual(table_of_contents.create_table_of_contents(array_of_contents, depth=2), expects)

def test_table_of_contents_with_colon(self):
array_of_contents = ['My header: 1']
expects = '\n* [My header: 1](#my-header-1)\n'

self.assertEqual(TableOfContents().create_table_of_contents(array_of_contents), expects)

def test_table_of_contents_with_dot(self):
array_of_contents = ['My.header 1.1']
expects = '\n* [My.header 1.1](#myheader-11)\n'

self.assertEqual(TableOfContents().create_table_of_contents(array_of_contents), expects)

def test_table_of_contents_with_back_slash(self):
array_of_contents = ['My\header 1']
expects = '\n* [My\header 1](#myheader-1)\n'

self.assertEqual(TableOfContents().create_table_of_contents(array_of_contents), expects)

def test_table_of_contents_with_hyphen(self):
array_of_contents = ['My-header-1 pop']
expects = '\n* [My-header-1 pop](#my-header-1-pop)\n'

self.assertEqual(TableOfContents().create_table_of_contents(array_of_contents), expects)

0 comments on commit cbd8bb6

Please sign in to comment.