Skip to content

Commit

Permalink
[Shock] - Added Jinja filter called 'pathRelativeToPage' to build URL…
Browse files Browse the repository at this point in the history
…s on top of page URL (Relates to #164) (#165)
  • Loading branch information
matthew-carroll authored Aug 5, 2024
1 parent 1b6c703 commit a3cfb31
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/static_shock/lib/src/plugins/jinja.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}

0 comments on commit a3cfb31

Please sign in to comment.