-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redirect to newly evaluated route on content creation (#232).
- Loading branch information
Showing
6 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,4 +113,4 @@ | |
color: #1c7fc7; | ||
text-decoration: underline; | ||
} | ||
</style> | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { env } from '../../generated/env.js'; | ||
|
||
export default function evaluateRoute(commitItem) { | ||
let content_type = window.location.hash.split("/")[1]; | ||
let filename = window.location.hash.split("/")[2]; | ||
let filepath = commitItem.file; | ||
let fields = JSON.parse(commitItem.contents); | ||
let default_route = "/" + content_type + "/" + filename; | ||
let route_pattern = env?.routes?.[content_type] ?? default_route; | ||
|
||
// Replace patterns with actual values | ||
let route = replaceRouteFields(route_pattern, fields); | ||
route = route.replace(':filename', filename); | ||
route = route.replace(':filepath', filepath); | ||
return route; | ||
} | ||
|
||
function replaceRouteFields(route, fields) { | ||
// Regular expression to match :fields(name) pattern | ||
const fieldRegex = /:fields\((\w+)\)/g; | ||
|
||
// Replace each match with the corresponding value from the fields object | ||
return route.replace(fieldRegex, (match, fieldName) => { | ||
if (fieldName in fields) { | ||
return slugify(fields[fieldName]); | ||
} | ||
// If the field is not found in the object, return the original match | ||
return match; | ||
}); | ||
} | ||
|
||
function slugify(str) { | ||
str = str.replace(/^\s+|\s+$/g, ''); // trim leading/trailing white space | ||
str = str.toLowerCase(); // convert string to lowercase | ||
str = str.replace(/[^a-z0-9 -]/g, '') // remove any non-alphanumeric characters | ||
.replace(/\s+/g, '-') // replace spaces with hyphens | ||
.replace(/-+/g, '-'); // remove consecutive hyphens | ||
return str; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters