Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Shock] - Added Jinja filter called 'pathRelativeToPage' to build URLs on top of page URL (Relates to #164) #165

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Changes from all 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
13 changes: 13 additions & 0 deletions packages/static_shock/lib/src/plugins/jinja.dart
Original file line number Diff line number Diff line change
@@ -162,6 +162,7 @@ class JinjaPageRenderer implements PageRenderer {
MapEntry("startsWith", _startsWith),
MapEntry("formatDateTime", _formatDateTime),
MapEntry("take", _take),
MapEntry("pathRelativeToPage", (String relativePath) => _pathRelativeToPage(page, relativePath)),
...filters.map((filterBuilder) {
final filter = filterBuilder(context);
return MapEntry<String, Function>(filter.$1, filter.$2);
@@ -273,4 +274,16 @@ class JinjaPageRenderer implements PageRenderer {

/// A Jinja filter that returns the first [count] items from the given list.
List _take(List incoming, int count) => incoming.sublist(0, min(count, incoming.length));

/// A Jinja filter (with a [Page] for context), which treats [relativePath] as a path
/// that's relative the [page], and returns the full URL path that combines the two.
///
/// Example:
/// - Page URL: `/posts/my-article/index.html`
/// - relativePath: `images/my-photo.png`
/// - return value: `/posts/my-article/images/my-photo.png`
String _pathRelativeToPage(Page page, String relativePath) {
final pageUrl = Uri.parse(page.url!);
return pageUrl.resolve(relativePath).path;
}
}
Loading