Skip to content

Commit

Permalink
fix for relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
alexclaes committed Feb 5, 2024
1 parent d537b6c commit f02e583
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
15 changes: 15 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const path = require('path');

module.exports = function (eleventyConfig) {
eleventyConfig.addPassthroughCopy('src/_assets/normalize.css');
eleventyConfig.addPassthroughCopy('src/_assets/styles.css');
Expand All @@ -8,4 +10,17 @@ module.exports = function (eleventyConfig) {
.getFilteredByGlob('src/uebungen/*.md')
.sort((a, b) => a.inputPath.localeCompare(b.inputPath));
});

eleventyConfig.addFilter('relativeUrl', (url, page) => {
if (!url.startsWith('/')) {
throw new Error('URL is already relative');
}
const relativeUrl = path.relative(page.filePathStem, url);

if (page.filePathStem === '/index') {
return path.relative('..', relativeUrl);
} else {
return relativeUrl;
}
});
};
4 changes: 2 additions & 2 deletions src/_includes/layout.njk
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ title: BasisQualifikation-IT
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/_assets/normalize.css">
<link rel="stylesheet" href="/_assets/styles.css">
<link rel="stylesheet" href="{{ '/_assets/normalize.css' | relativeUrl(page)}}">
<link rel="stylesheet" href="{{ '/_assets/styles.css' | relativeUrl(page)}}">
<title>{{ title }}</title>
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion src/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ layout: layout.njk
<h2>Übungen</h2>
<ul>
{%- for item in collections.uebungen -%}
<li><a href="{{ item.url }}">{{ item.data.title }}</a></li>
<li><a href="{{ item.url | relativeUrl(page) }}">{{ item.data.title }}</a></li>
{%- endfor -%}
</ul>

0 comments on commit f02e583

Please sign in to comment.