Skip to content
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
6 changes: 3 additions & 3 deletions layouts/test/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@ <h1>{{ $title }}</h1>
{{ $tesAbsPath := .RelPermalink }}

<script>
const testId = {{ $test.id | jsonify }};
const testId = {{$test.id}};
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.

high

Removing the jsonify filter will cause a JavaScript syntax error because the value (which is a string) will be rendered without surrounding quotes (e.g., const testId = some-id;). Using jsonify is the idiomatic way to safely pass Hugo variables to JavaScript as it handles both quoting and escaping.

Suggested change
const testId = {{$test.id}};
const testId = {{ $test.id | jsonify }};

const testAbsPath = "{{$tesAbsPath}}";
const build = {{ $build | jsonify }};
const build = {{$build}};
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.

high

The build variable is a string (e.g., "development" or "production"). Removing jsonify without adding quotes will result in invalid JavaScript (e.g., const build = development;), which will throw a ReferenceError in the browser.

Suggested change
const build = {{$build}};
const build = {{ $build | jsonify }};



// Event Listeners
Expand All @@ -377,7 +377,7 @@ <h1>{{ $title }}</h1>
window.addEventListener("academyContextReady", () => {
if (isProdBuild && window.academyContext?.RegistrationData?.id) {
// redirect to test page
const curriculaRoot = {{ .RelPermalink | strings.TrimSuffix "/" | jsonify }};
const curriculaRoot = window.location.pathname.split("/").slice(0, 5).join("/");
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

Using window.location.pathname.split("/").slice(0, 5).join("/") is brittle as it relies on a fixed URL depth. This logic will break if the URL structure changes or if the site is hosted under a different base path. It is safer to use Hugo's .RelPermalink which is determined at build time based on the content structure.

Suggested change
const curriculaRoot = window.location.pathname.split("/").slice(0, 5).join("/");
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