Skip to content

Commit de066c1

Browse files
authored
Merge pull request #749 from dwrolvink/master
#730 fix breadcrumb links and names
2 parents 59b48e9 + 5908c61 commit de066c1

File tree

2 files changed

+61
-26
lines changed

2 files changed

+61
-26
lines changed

obsidianhtml/controller/ConvertVault.py

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55

66
import regex as re # regex string finding/replacing
77

8+
from urllib.parse import unquote
9+
from pathlib import Path
10+
811
from .. import md2html
912

10-
from ..lib import CreateStaticFilesFolders, WriteFileLog, simpleHash, get_html_url_prefix, retain_reference, OpenIncludedFile
13+
from ..lib import CreateStaticFilesFolders, WriteFileLog, simpleHash, get_html_url_prefix, retain_reference, OpenIncludedFile, slugify
1114

1215
from ..compiler.Templating import PopulateTemplate
1316
from ..core.PicknickBasket import PicknickBasket
@@ -228,6 +231,16 @@ def convert_markdown_to_html(pb):
228231
if pb.gc("toggles/features/embedded_search/enabled", cached=True):
229232
esearch = EmbeddedSearch(json_data=pb.search.OutputJson())
230233

234+
# prepare lookup to translate slugified folder names to their original
235+
folder_og_name_lut = {}
236+
for file in pb.index.files.keys():
237+
file_path = pb.index.files[file].path["markdown"]["file_relative_path"]
238+
for el in file_path.as_posix().split("/")[:-1]:
239+
slug_el = slugify(el)
240+
if slug_el not in folder_og_name_lut:
241+
print(slug_el, el)
242+
folder_og_name_lut[slug_el] = el
243+
231244
print("\t> SECOND PASS HTML")
232245

233246
for fo in pb.index.files.values():
@@ -273,48 +286,63 @@ def convert_markdown_to_html(pb):
273286
html = md2html.insert_tags_footer(pb, html, tags, fo.md.metadata)
274287

275288
# add breadcrumbs
289+
# ------------------------------------------------------------------------
276290
if pb.gc("toggles/features/breadcrumbs/enabled", cached=True):
277-
if node["url"] == "/index.html":
291+
html_url_prefix = pb.gc("html_url_prefix", cached=True)
292+
293+
if node["url"] == f"{html_url_prefix}/index.html":
294+
# Don't create breadcrumbs for the homepage
278295
snippet = ""
296+
279297
else:
280-
html_url_prefix = pb.gc("html_url_prefix", cached=True)
298+
# loop through all/links/along/the_way.html
299+
300+
# set first element to be home
281301
parts = [f'<a href="{html_url_prefix}/" style="color: rgb(var(--normal-text-color));">Home</a>']
282302

283-
previous_url = ""
284303
subpaths = node["url"].replace(".html", "").split("/")[1:]
285-
match_subpaths = subpaths
286304

287305
if pb.gc("toggles/force_filename_to_lowercase", cached=True):
288-
match_subpaths = [x.lower() for x in subpaths]
306+
subpaths = [x.lower() for x in subpaths]
289307

290308
if html_url_prefix:
291-
subpaths = subpaths[1:]
292-
match_subpaths = match_subpaths[1:]
309+
# remove the parts that are part of the prefix
310+
prefix_amount = len(html_url_prefix.split("/")) - 1
311+
subpaths = subpaths[prefix_amount:]
293312

294-
for i, msubpath in enumerate(match_subpaths):
295-
if i == len(msubpath) - 1:
296-
if node["url"] != previous_url:
297-
parts.append(f'<a href="{node["url"]}" ___COLOR___ >{subpaths[i]}</a>')
298-
continue
313+
previous_url = ""
314+
for i, subpath in enumerate(subpaths):
315+
subpath = unquote(subpath)
316+
if subpath in pb.index.network_tree.node_lookup:
317+
lnode = pb.index.network_tree.node_lookup[subpath]
318+
elif subpath in pb.index.network_tree.node_lookup_slug:
319+
lnode = pb.index.network_tree.node_lookup_slug[subpath]
299320
else:
300-
url = None
301-
if msubpath in pb.index.network_tree.node_lookup:
302-
url = pb.index.network_tree.node_lookup[msubpath]["url"]
303-
elif msubpath in pb.index.network_tree.node_lookup_slug:
304-
url = pb.index.network_tree.node_lookup_slug[msubpath]["url"]
305-
else:
306-
parts.append(f'<span style="color: #666;">{subpaths[i]}</span>')
307-
previous_url = ""
308-
continue
309-
if url != previous_url:
310-
parts.append(f'<a href="{url}" ___COLOR___>{subpaths[i]}</a>')
311-
previous_url = url
321+
# try finding folder with same name in markdown folder
322+
# to get proper capitalization, even if we use slugify
323+
name = unquote(subpaths[i])
324+
if name in folder_og_name_lut:
325+
name = folder_og_name_lut[name]
326+
327+
parts.append(f'<span style="color: #666;">{name}</span>')
328+
previous_url = ""
312329
continue
313330

331+
url = lnode["url"]
332+
name = lnode["name"]
333+
334+
# in the case of folder notes, we have the folder and note name being the
335+
# same, we don't want to print this twice in the breadcrumbs
336+
if url != previous_url:
337+
parts.append(f'<a href="{url}" ___COLOR___>{name}</a>')
338+
previous_url = url
339+
340+
# set all links to be normal text color except for the last link
314341
parts[-1] = parts[-1].replace("___COLOR___", "")
315342
for i, link in enumerate(parts):
316343
parts[i] = link.replace("___COLOR___", 'style="color: var(--normal-text-color);"')
317344

345+
# combine parts into snippet
318346
snippet = " / ".join(parts)
319347
snippet = f"""
320348
<div style="width:100%; text-align: right;display: block;margin: 0.5rem;">

obsidianhtml/lib.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,14 @@ def ConvertTitleToMarkdownId(title):
9696

9797

9898
def slugify_path(value, separator="-", unicode=False, skip_chars_re=r"/\."):
99-
return slugify(value, separator, unicode, skip_chars_re)
99+
# avoids "test?.html" turning into "test-.html" instead of "test.html"
100+
suffix = ""
101+
if value.endswith(".html"):
102+
value = re.sub(r'\.html$', '', value)
103+
suffix = ".html"
104+
105+
slugified_value = slugify(value, separator, unicode, skip_chars_re)
106+
return f'{slugified_value}{suffix}'
100107

101108

102109
def slugify(value, separator="-", unicode=False, skip_chars_re=""):

0 commit comments

Comments
 (0)