Skip to content

Commit a6bc00e

Browse files
committed
Add tests and fix issue in passing options
1 parent 8be8081 commit a6bc00e

7 files changed

Lines changed: 99 additions & 6 deletions

File tree

src/project/types/website/website-search.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -617,22 +617,28 @@ export async function websiteSearchIncludeInHeader(
617617
}
618618
});
619619

620-
const searchOptionsJson = JSON.stringify(options, null, 2);
621-
const searchOptionsScript =
622-
`<script id="quarto-search-options" type="application/json">${searchOptionsJson}</script>`;
623-
const includes = [searchOptionsScript];
620+
const includes: string[] = [];
624621

622+
// Process all Algolia configuration and scripts
625623
if (options[kAlgolia]) {
626624
includes.push(kAlogioSearchApiScript);
627-
if (options[kAlgolia]?.[kAnalyticsEvents]) {
625+
if (options[kAlgolia][kAnalyticsEvents]) {
628626
const cookieConsent = cookieConsentEnabled(project);
629-
// Pass cookie consent status to JavaScript for Algolia Insights configuration
627+
// Set cookie consent flag for JavaScript configuration
630628
options[kAlgolia][kCookieConsentEnabled] = cookieConsent;
629+
// Add Algolia Insights scripts with proper consent handling
631630
includes.push(algoliaSearchInsightsScript(cookieConsent));
632631
includes.push(autocompleteInsightsPluginScript(cookieConsent));
633632
}
634633
}
635634

635+
// Serialize search options to JSON after all modifications
636+
const searchOptionsJson = JSON.stringify(options, null, 2);
637+
const searchOptionsScript =
638+
`<script id="quarto-search-options" type="application/json">${searchOptionsJson}</script>`;
639+
// Prepend search options script to beginning of includes
640+
includes.unshift(searchOptionsScript);
641+
636642
Deno.writeTextFileSync(websiteSearchScript, includes.join("\n"));
637643
return websiteSearchScript;
638644
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.quarto/
2+
**/*.quarto_ipynb
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
project:
2+
type: website
3+
4+
website:
5+
title: "Algolia Without Cookie Consent Test"
6+
search:
7+
algolia:
8+
application-id: "TEST_APP_ID"
9+
search-only-api-key: "TEST_API_KEY"
10+
index-name: "test-index"
11+
analytics-events: true
12+
# This is the default and should act as such
13+
# cookie-consent: false
14+
15+
format:
16+
html:
17+
theme: cosmo
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: "Algolia Search Without Cookie Consent"
3+
_quarto:
4+
tests:
5+
html:
6+
ensureHtmlElements:
7+
# Ensure search options script exists
8+
- ['script#quarto-search-options']
9+
ensureFileRegexMatches:
10+
-
11+
# Cookie consent should be false
12+
- '"cookie-consent-enabled":\s*false'
13+
# Scripts should load immediately with type="text/javascript"
14+
- '<script[^>]*type="text/javascript">[\s\S]+search-insights'
15+
-
16+
# Verify scripts do NOT have cookie-consent attribute
17+
- '<script[^>]*cookie-consent[^>]+>[\s\S]*search-insights'
18+
---
19+
20+
This test verifies that when `cookie-consent` is NOT configured in the website settings:
21+
22+
1. The Algolia search options JSON contains `"cookie-consent-enabled": false` (or the key is absent)
23+
2. Algolia Insights scripts are loaded immediately with `type="text/javascript"`
24+
3. Scripts are not deferred behind cookie consent (no `cookie-consent="tracking"` attribute)
25+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.quarto/
2+
**/*.quarto_ipynb
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
project:
2+
type: website
3+
4+
website:
5+
title: "Algolia With Cookie Consent Test"
6+
search:
7+
algolia:
8+
application-id: "TEST_APP_ID"
9+
search-only-api-key: "TEST_API_KEY"
10+
index-name: "test-index"
11+
analytics-events: true
12+
cookie-consent: true
13+
14+
format:
15+
html:
16+
theme: cosmo
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: "Algolia Search With Cookie Consent"
3+
_quarto:
4+
tests:
5+
html:
6+
ensureHtmlElements:
7+
-
8+
# Ensure search options script exists
9+
- 'script#quarto-search-options'
10+
# Cookie consent element are loaded
11+
- script[src$='cookie-consent.js']
12+
ensureFileRegexMatches:
13+
-
14+
# Cookie consent should be enabled
15+
- '"cookie-consent-enabled":\s*true'
16+
# Scripts should be deferred with cookie-consent attribute
17+
- 'type="text/plain"[^>]*cookie-consent="tracking"[^>]*>[\s\S]*search-insights'
18+
---
19+
20+
This test verifies that when `cookie-consent` is configured in the website settings:
21+
22+
1. The Algolia search options JSON contains `"cookie-consent-enabled": true`
23+
2. Algolia Insights scripts are deferred with `type="text/plain" cookie-consent="tracking"`
24+
3. Scripts only execute after user grants "tracking" consent
25+
4. Cookie consent UI elements are present on the page

0 commit comments

Comments
 (0)