Skip to content
Closed
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
10 changes: 5 additions & 5 deletions layouts/test/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,13 @@ <h1>{{ $title }}</h1>

{{ $test := partial "test/single.html" . }}
{{ $tesAbsPath := .RelPermalink }}
{{ $curriculaRoot := .RelPermalink | strings.TrimSuffix "/" | strings.TrimSuffix "/test" }}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation to derive curriculaRoot using strings.TrimSuffix is a bit fragile. It could lead to incorrect paths if a directory name happens to end with "test" but is not exactly "test" (e.g., /some/path/latest-test/).

A more robust approach would be to use a regular expression to ensure you're removing the /test segment only when it appears at the very end of the path.

Consider this alternative:

{{ $curriculaRoot := .RelPermalink | replaceRE "/test/?$" "" }}

This uses replaceRE to remove /test or /test/ from the end of the .RelPermalink, which is safer and more explicit about the intent.

Suggested change
{{ $curriculaRoot := .RelPermalink | strings.TrimSuffix "/" | strings.TrimSuffix "/test" }}
{{ $curriculaRoot := .RelPermalink | replaceRE "/test/?$" "" }}


<script>
const testId = {{ $test.id | jsonify }};
const testAbsPath = "{{$tesAbsPath}}";
const build = {{ $build | jsonify }};
const testId = {{ $test.id | jsonify | safeJS }};
const testAbsPath = {{ $tesAbsPath | jsonify | safeJS }};
const build = {{ $build | jsonify | safeJS }};
const curriculaRoot = {{ $curriculaRoot | jsonify | safeJS }};


// Event Listeners
Expand All @@ -376,8 +378,6 @@ <h1>{{ $title }}</h1>
// Initialize when context is ready
window.addEventListener("academyContextReady", () => {
if (isProdBuild && window.academyContext?.RegistrationData?.id) {
// redirect to test page
const curriculaRoot = {{ .RelPermalink | strings.TrimSuffix "/" | jsonify }};
window.location.replace(`${curriculaRoot}/test?id=${testId}&registration_id=${window.academyContext?.RegistrationData?.id}&test_abs_path=${testAbsPath}`);
return;
}
Expand Down
Loading