Skip to content

Commit 72fccf9

Browse files
committed
feat(build): shortcode to mark version requirements
1 parent dd673a1 commit 72fccf9

File tree

4 files changed

+142
-3
lines changed

4 files changed

+142
-3
lines changed

docs/nodes/builtin/mapper.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,23 @@ the correct data types, the names are simply incompatible.
3333
## Liquid
3434

3535
The Shopify-developed engine is a powerful and flexible templating language that allows for complex data
36-
transformations. It supports loops, conditionals, filters, and more. \
36+
transformations. It supports loops, conditionals, filters, and more.
3737
The [Liqp](https://github.com/bkiers/Liqp) library provides the Liquid templating engine. Therefore, the behavior may
3838
not mimic the original Ruby-based Liquid engine perfectly. For the most part, it should work as expected though.
3939

4040
As Liquid is a nicely documented language with a ton of features, please refer to the official documentation:
41-
https://shopify.github.io/liquid/basics/introduction/
41+
[https://shopify.github.io/liquid/basics/introduction/]
4242

4343
Apart from the generic features, Snoty also provides some custom filters and tags:
4444

45-
1. `unidiff` filter
45+
1. `unidiff` filter <!-- md:version 0.5.0 -->
4646
- This filter is used to convert a list of dictionaries into a unified diff format. It takes a list of dictionaries
4747
and returns a string representing the unified diff.
4848
- Example:
4949
```liquid
5050
{% assign diff = diff.change.description.old | unidiff diff.change.description.new %}
5151
```
52+
2. `format` filter <!-- md:version next -->
5253
5354
## Examples
5455
### List all changes

docs/stylesheets/badges.css

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright (c) 2016-2023 Martin Donath <[email protected]>
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a
5+
copy of this software and associated documentation files (the "Software"),
6+
to deal in the Software without restriction, including without limitation
7+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
and/or sell copies of the Software, and to permit persons to whom the
9+
Software is furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+
DEALINGS
21+
*/
22+
23+
.md-typeset .mdx-badge {
24+
font-size: 0.85em;
25+
}
26+
27+
.md-typeset .mdx-badge--right {
28+
float: right;
29+
margin-left: 0.35em;
30+
}
31+
32+
.md-typeset .mdx-badge__icon {
33+
padding: 0.25em;
34+
background: var(--md-accent-fg-color--transparent);
35+
border-start-start-radius: 0.25em;
36+
border-end-start-radius: 0.25em;
37+
}
38+
39+
.md-typeset .mdx-badge__icon:last-child {
40+
border-radius: 0.25em;
41+
}
42+
43+
.md-typeset .mdx-badge__text {
44+
padding: 0.25em 0.5em;
45+
border-start-end-radius: 0.25em;
46+
border-end-end-radius: 0.25em;
47+
box-shadow: 0 0 0 1px inset var(--md-accent-fg-color--transparent);
48+
}

mkdocs.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ markdown_extensions:
3434
- pymdownx.blocks.caption
3535
- toc:
3636
permalink: true
37+
- pymdownx.emoji:
38+
emoji_index: !!python/name:material.extensions.emoji.twemoji
39+
emoji_generator: !!python/name:material.extensions.emoji.to_svg
3740

3841
extra:
3942
homepage: https://snoty.me
@@ -43,6 +46,10 @@ extra:
4346
extra_css:
4447
- stylesheets/logo.css
4548
- stylesheets/extra.css
49+
- stylesheets/badges.css
50+
51+
hooks:
52+
- ./src/hooks/shortcodes.py
4653

4754
nav:
4855
- Welcome: index.md

src/hooks/shortcodes.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Copyright (c) 2016-2025 Martin Donath <[email protected]>
2+
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to
5+
# deal in the Software without restriction, including without limitation the
6+
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7+
# sell copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19+
# IN THE SOFTWARE.
20+
21+
from __future__ import annotations
22+
23+
import posixpath
24+
import re
25+
26+
from mkdocs.config.defaults import MkDocsConfig
27+
from mkdocs.structure.files import File, Files
28+
from mkdocs.structure.pages import Page
29+
from re import Match
30+
31+
# -----------------------------------------------------------------------------
32+
# Hooks
33+
# -----------------------------------------------------------------------------
34+
35+
def on_page_markdown(
36+
markdown: str, *, page: Page, config: MkDocsConfig, files: Files
37+
):
38+
39+
# Replace callback
40+
def replace(match: Match):
41+
type, args = match.groups()
42+
args = args.strip()
43+
if type == "version":
44+
return _badge_for_version(args, page, files)
45+
46+
raise RuntimeError(f"Unknown shortcode: {type}")
47+
48+
# Find and replace all external asset URLs in current page
49+
return re.sub(
50+
r"<!-- md:(\w+)(.*?) -->",
51+
replace, markdown, flags = re.I | re.M
52+
)
53+
54+
def _badge(icon: str, text: str = "", type: str = ""):
55+
print("makin")
56+
classes = f"mdx-badge mdx-badge--{type}" if type else "mdx-badge"
57+
return "".join([
58+
f"<span class=\"{classes}\">",
59+
*([f"<span class=\"mdx-badge__icon\">{icon}</span>"] if icon else []),
60+
*([f"<span class=\"mdx-badge__text\">{text}</span>"] if text else []),
61+
f"</span>",
62+
])
63+
64+
def _badge_for_version(text: str, page: Page, files: Files):
65+
spec = text
66+
67+
repo_url = "https://github.com/SnotyMe/snoty-backend"
68+
69+
if spec == "next":
70+
url = f"{repo_url}/tree/main"
71+
else:
72+
url = f"{repo_url}/releases/tag/{spec}"
73+
74+
text = f"[{spec}]({url})" + '{: target="_blank"}' if spec else ""
75+
# dev is on a feature branch => not guaranteed to land in the next version => no link (could be anywhere)
76+
if spec == "dev":
77+
text = spec
78+
79+
icon = "material-tag-outline"
80+
return _badge(
81+
icon = f"[:{icon}:]('Available from version {spec}')",
82+
text = text
83+
)

0 commit comments

Comments
 (0)