Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
22 changes: 21 additions & 1 deletion django/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions django/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ django-cors-headers = "^3.7.0"
boto3 = "^1.17.47"
bleach = "^3.3.0"
markdown-it-py = {version = "2.1.0", extras = ["linkify"]}
mdit-py-plugins = "0.4.2"
abyss = {git = "https://github.com/akx/abyss.git", rev = "4352e557f25a303f718ec1bf82ea0ca545ebc077"}
lxml = "^4.9.1"
pygments = "^2.16.1"
Expand Down
4 changes: 2 additions & 2 deletions django/thunderstore/api/cyberstorm/tests/test_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def test_readme_api_view__prerenders_markup(api_client: APIClient) -> None:
f"/api/cyberstorm/package/{v.package.namespace}/{v.package.name}/latest/readme/",
)
actual = response.json()

assert actual["html"] == "<h1>Very <strong>strong</strong> header</h1>\n"
expected_html = '<h1 id="user-content-very-strong-header">Very <strong>strong</strong> header</h1>\n'
assert actual["html"] == expected_html


@pytest.mark.django_db
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
this is a description
""".strip()

MARKDOWN_RENDERED = """<h1>Test markdown</h1>
MARKDOWN_RENDERED = """<h1 id="user-content-test-markdown">Test markdown</h1>
<p>this is a description</p>
"""

Expand Down
39 changes: 39 additions & 0 deletions django/thunderstore/frontend/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,45 @@
{# CSS and JS #}
<link rel="stylesheet" href="{% static 'css/all.css' %}">
<script src="{% static 'js/all.js' %}"></script>

{# Anchor navigation JS #}
<script>
function slugger(text) {
let slug = text.replace(/\s+/g, "-");
slug = slug.replace(/[^\p{L}\p{N}_-]+/gu, "");
return "user-content-" + slug.toLowerCase();
}

function scrollToHash(hash) {
if (!hash) return;

const targetId = hash.slice(1);
const normalizedTarget = targetId.startsWith('user-content-') ? targetId : slugger(targetId);
const target = document.getElementById(normalizedTarget);

if (!target) return;

target.scrollIntoView({ behavior: "auto", block: "start" });
}
Comment thread
x753 marked this conversation as resolved.

document.addEventListener('click', (e) => {
const link = e.target.closest('a[href^="#"]');
if (!link) return;

e.preventDefault();
const hash = link.getAttribute('href');
if (hash) {
scrollToHash(hash);
if (location.hash !== hash)
{
history.pushState(null, "", hash);
}
}
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

document.addEventListener("DOMContentLoaded", () => scrollToHash(location.hash));
window.addEventListener("hashchange", () => scrollToHash(location.hash));
</script>
</head>
<body>
{% if community.background_image %}
Expand Down
12 changes: 6 additions & 6 deletions django/thunderstore/markdown/allowed_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@
"img": ["src", "width", "height", "alt", "align"],
"th": ["align", "rowspan", "colspan"],
"td": ["align", "rowspan", "colspan"],
"h1": ["align"],
"h2": ["align"],
"h3": ["align"],
"h4": ["align"],
"h5": ["align"],
"h6": ["align"],
"h1": ["align", "id"],
"h2": ["align", "id"],
"h3": ["align", "id"],
"h4": ["align", "id"],
"h5": ["align", "id"],
"h6": ["align", "id"],
"p": ["align"],
"details": ["open"],
}
12 changes: 11 additions & 1 deletion django/thunderstore/markdown/templatetags/markdownify.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe
from markdown_it import MarkdownIt
from mdit_py_plugins.anchors import anchors_plugin

from thunderstore.markdown.allowed_tags import (
ALLOWED_ATTRIBUTES,
ALLOWED_PROTOCOLS,
ALLOWED_TAGS,
)


def slugger(text: str) -> str:
import re

slug = re.sub(r"\s+", "-", text)
slug = re.sub(r"[^\w\-]", "", slug)
Comment thread
x753 marked this conversation as resolved.
return f"user-content-{slug.lower()}"
Comment thread
x753 marked this conversation as resolved.


register = template.Library()
md = MarkdownIt("gfm-like")
md = MarkdownIt("gfm-like").use(anchors_plugin, slug_func=slugger, max_level=6)


def render_markdown(value: str):
Expand Down
Loading