Skip to content

Commit

Permalink
add wikilinks support for books
Browse files Browse the repository at this point in the history
  • Loading branch information
wang.jianbo committed Oct 15, 2024
1 parent ba57b4e commit b84c110
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
12 changes: 8 additions & 4 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{% extends "base.html" %}
{% import "macros/wikilinks.html" as wikilinks %}

{% block content %}
<article class="mt-16">
{{ section.content | safe }}
{{ wikilinks::render(section=section) }}
</article>

{% if config.extra.list_pages %}
Expand All @@ -14,17 +15,20 @@
{% endif %}

<ul class="titleList">
{% for page in pages %}
{% for page in pages %}
<li>
<a href="{{ page.permalink | safe }}">{{ page.title }}</a>
<br />
{{ page.description }}
</li>
{% endfor %}
{% endfor %}
</ul>

{% if paginator %}
<div class="metaData">{% if paginator.previous %}<a href="{{ paginator.first }}"></a> &nbsp <a href="{{ paginator.previous }}"><</a>{% endif %} &nbsp {{ paginator.current_index }} / {{ paginator.number_pagers }} &nbsp {% if paginator.next %}<a href="{{ paginator.next }}">></a> &nbsp <a href="{{ paginator.last }}"></a>{% endif %}</div>
<div class="metaData">{% if paginator.previous %}<a href="{{ paginator.first }}"></a> &nbsp <a
href="{{ paginator.previous }}">
<< /a>{% endif %} &nbsp {{ paginator.current_index }} / {{ paginator.number_pagers }} &nbsp {% if paginator.next
%}<a href="{{ paginator.next }}">></a> &nbsp <a href="{{ paginator.last }}"></a>{% endif %}</div>
{% endif %}

{% endif %}
Expand Down
21 changes: 21 additions & 0 deletions templates/macros/wikilinks.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% macro render(section) %}
{% set first_bracket = "[[" %}
{% set last_bracket = "]]" %}

{% set wikiregex = "\\[\\[([^/]+?)\\]\\]" %}

{% set_global processed_content = section.content %}

{% for wikilink in section.content | split(pat=first_bracket) | slice(start=1) %}
{% set link_parts = wikilink | split(pat=last_bracket) %}
{% if link_parts | length >= 1 %}
{% set link_content = link_parts | first %}
{% set link_content = link_content | replace(from="?", to="\\?") %}
{% set wikilink_pattern = first_bracket ~ link_content ~ last_bracket %}
{% set url = get_url(path="@/books/" ~ link_content ~ ".md") %}
{% set link = "<a href=" ~ url ~ ">" ~ link_content ~ "</a>" %}
{% set_global processed_content=processed_content | replace(from=wikilink_pattern, to=link) %}
{% endif %}
{% endfor %}
{{ processed_content | safe }}
{% endmacro %}

0 comments on commit b84c110

Please sign in to comment.