From f85e36bb59a39e7e2e30433521c79f216c4504c6 Mon Sep 17 00:00:00 2001 From: Naomi Pentrel <5212232+npentrel@users.noreply.github.com> Date: Thu, 9 Jan 2025 13:42:49 +0100 Subject: [PATCH] Try to fix js for staging --- assets/js/howtos.js | 191 ------------------------- assets/js/models.js | 8 +- assets/js/tutorials.js | 4 +- layouts/docs/landing.html | 8 -- layouts/docs/tutorials.html | 14 +- layouts/partials/page-description.html | 58 ++++---- layouts/partials/scripts.html | 23 +-- 7 files changed, 61 insertions(+), 245 deletions(-) delete mode 100644 assets/js/howtos.js diff --git a/assets/js/howtos.js b/assets/js/howtos.js deleted file mode 100644 index 0a6b41240c..0000000000 --- a/assets/js/howtos.js +++ /dev/null @@ -1,191 +0,0 @@ -const { TypesenseInstantSearchAdapter, instantsearch } = window; -const observer = lozad(); - -const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({ - server: { - apiKey: "oRW875O3vjeV3qX4ENl1iIA0u2IRDbTQ", // Be sure to use an API key that only allows search operations - nodes: [ - { - host: "cgnvrk0xwyj9576lp-1.a1.typesense.net", - port: "443", - protocol: "https", - }, - ], - cacheSearchResultsForSeconds: 2 * 60, // Cache search results from server. Defaults to 2 minutes. Set to 0 to disable caching. - }, - // The following parameters are directly passed to Typesense's search API endpoint. - // So you can pass any parameters supported by the search endpoint below. - // query_by is required. - additionalSearchParameters: { - query_by: "resource,title,description", - sort_by: "featured:asc,date:desc", - }, -}); - -const searchClient = typesenseInstantsearchAdapter.searchClient; - -const search = instantsearch({ - indexName: "tutorials", - searchClient, -}); - - -const customRefinementList = instantsearch.connectors.connectRefinementList( - ({ items, refine, widgetParams }, isFirstRender) => { - - const container = document.getElementById(widgetParams.container); - - if (isFirstRender) { - container.addEventListener("click", ({ target }) => { - const input = target.closest("input"); - - if (input) { - let refinements = search.helper.state.disjunctiveFacetsRefinements["platformarea"]; - if (refinements.length) { - if (refinements[0] == input.name) { - refine(refinements[0]); - } else { - refine(refinements[0]); - refine(input.name) - } - } else { - refine(input.name) - } - } - }); - - return; - } - - const list = widgetParams.items.map(({ label: staticLabel, value: staticValue }) => { - const elem = items.find(({ label }) => label === staticValue); - - let count = 0; - let isRefined = false; - if (elem) { - count = elem.count; - isRefined = elem.isRefined; - } - - return ` -
  • - -
  • - `; - }); - - container.innerHTML = ` - - `; - }, -); - -let refinementLists = [customRefinementList({ - container: "platformarea-list", - attribute: "platformarea", - operator: "or", - sortBy: ["name:asc"], - items: [ - { label: "Data Management", value: "data" }, - { label: "Machine Learning", value: "ml" }, - { label: "Control hardware", value: "core" }, - { label: "Fleet Management", value: "fleet" }, - { label: "Integrate other hardware", value: "registry" }, - { label: "Mobility", value: "mobility" }, - ], -}), -customRefinementList({ - container: "resource-list", - attribute: "resource", - operator: "or", - sortBy: ["name:asc"], - items: [ - { label: "tutorial" }, - { label: "how-to" }, - { label: "quickstart" }, - { label: "blogpost" }, - { label: "codelab" }, - ], -})] - -let searchWidgets = [ - instantsearch.widgets.hits({ - container: "#hits", - templates: { - item: ` -
    - -
    -
    {{title}}
    -
    - {{#description}}

    {{description}}

    {{/description}} -
    -
    -
    -
    -`, - }, - }), - instantsearch.widgets.configure({ - hitsPerPage: 12, - }), - instantsearch.widgets.pagination({ - container: "#pagination", - }), -]; - -search.addWidgets(refinementLists); - -search.start(); -// Only show guide & howtos -search.addWidgets([{ - init: function(options) { - options.helper.toggleRefinement('resource', 'how-to'); - options.helper.toggleRefinement('resource', 'quickstart'); - } -}]); - -let widgetsAdded = false; - -search.on("render", function () { - if (search.helper.state.disjunctiveFacetsRefinements.platformarea.length) { - if (!widgetsAdded) { - widgetsAdded = true; - search.addWidgets(searchWidgets); - document.getElementById("how-to-paths").classList.add("isHidden"); - } - } else { - if (widgetsAdded) { - widgetsAdded = false; - search.removeWidgets(searchWidgets); - document.getElementById("how-to-paths").classList.remove("isHidden"); - } - } - - if ( - search.helper.state.facetsRefinements && - search.helper.state.disjunctiveFacetsRefinements.platformarea - ) { - document - .querySelectorAll(".pill-component") - .forEach((e) => - search.helper.state.disjunctiveFacetsRefinements.platformarea.includes( - e.textContent, - ) - ? e.classList.add("pill-highlight") - : e.classList.remove("pill-highlight"), - ); - } - - observer.observe(); -}); diff --git a/assets/js/models.js b/assets/js/models.js index 081a88aa47..403ffdc46b 100644 --- a/assets/js/models.js +++ b/assets/js/models.js @@ -43,7 +43,7 @@ if (api == "") { }; itemtemplate = `

    {{#helpers.highlight}}{ "attribute": "api" }{{/helpers.highlight}}

    -

    {{#helpers.highlight}}{ "attribute": "model" }{{/helpers.highlight}}

    +

    {{#helpers.highlight}}{ "attribute": "model" }{{/helpers.highlight}}

    {{#helpers.highlight}}{ "attribute": "description" }{{/helpers.highlight}}
    `; } else { @@ -52,7 +52,7 @@ if (api == "") { hitsPerPage: 5, }; itemtemplate = ` -

    {{#helpers.highlight}}{ "attribute": "model" }{{/helpers.highlight}}

    +

    {{#helpers.highlight}}{ "attribute": "model" }{{/helpers.highlight}}

    {{#helpers.highlight}}{ "attribute": "description" }{{/helpers.highlight}}
    `; } @@ -140,7 +140,7 @@ if (mlmodel) { hitsPerPage: 5, }; itemtemplateML = ` -

    {{#helpers.highlight}}{ "attribute": "model_id" }{{/helpers.highlight}}

    +

    {{#helpers.highlight}}{ "attribute": "model_id" }{{/helpers.highlight}}

    {{#helpers.highlight}}{ "attribute": "type" }{{/helpers.highlight}}
    {{#helpers.highlight}}{ "attribute": "framework" }{{/helpers.highlight}}
    {{#helpers.highlight}}{ "attribute": "description" }{{/helpers.highlight}}
    @@ -228,7 +228,7 @@ if (scripts) { hitsPerPage: 5, }; itemtemplateScripts = ` -

    {{#helpers.highlight}}{ "attribute": "model_id" }{{/helpers.highlight}}

    +

    {{#helpers.highlight}}{ "attribute": "model_id" }{{/helpers.highlight}}

    {{#helpers.highlight}}{ "attribute": "description" }{{/helpers.highlight}}
    `; diff --git a/assets/js/tutorials.js b/assets/js/tutorials.js index 394d132772..b1eed5afd8 100644 --- a/assets/js/tutorials.js +++ b/assets/js/tutorials.js @@ -1,3 +1,5 @@ +import params from "@params"; + const { TypesenseInstantSearchAdapter, instantsearch } = window; const observer = lozad(); @@ -97,7 +99,7 @@ search.addWidgets([ templates: { item: `
    - + {{#webm}}
    diff --git a/layouts/docs/landing.html b/layouts/docs/landing.html index aab8d630c8..75b635318b 100644 --- a/layouts/docs/landing.html +++ b/layouts/docs/landing.html @@ -38,13 +38,5 @@ {{ partial "scripts.html" . }} - - - - - {{ $jsTutorials := resources.Get "js/tutorials.js" }} - {{ $jsTutorials := $jsTutorials | minify }} - - diff --git a/layouts/docs/tutorials.html b/layouts/docs/tutorials.html index 0ad56c5def..91c6d7084c 100644 --- a/layouts/docs/tutorials.html +++ b/layouts/docs/tutorials.html @@ -118,8 +118,20 @@

    Javascript

    {{ $jsTutorials := resources.Get "js/tutorials.js" }} + {{ if (eq (substr .Site.BaseURL -1) "/" ) }} + {{- $opts := dict + "params" (dict "baseURL" (substr .Site.BaseURL 0 -1 )) + -}} + {{- $jsTutorials = $jsTutorials | js.Build $opts -}} {{ $jsTutorials := $jsTutorials | minify }} - + {{ else }} + {{- $opts := dict + "params" (dict "baseURL" .Site.BaseURL) + -}} + {{- $jsTutorials = $jsTutorials | js.Build $opts -}} + {{ $jsTutorials := $jsTutorials | minify }} + + {{ end }} diff --git a/layouts/partials/page-description.html b/layouts/partials/page-description.html index ff652929c4..791a4b446b 100644 --- a/layouts/partials/page-description.html +++ b/layouts/partials/page-description.html @@ -1,35 +1,35 @@ -{{ $recentlyChanged := (lt (sub now.Unix .Lastmod.Unix) 3600) }} +{{- $recentlyChanged := (lt (sub now.Unix .Lastmod.Unix) 3600) -}} -{{if .Params.metaDescription }} - {{if gt (len .Params.metaDescription) 158}} +{{- if .Params.metaDescription -}} + {{if gt (len .Params.metaDescription) 158 -}} {{- warnf "Page description too long (%d) - should be 50-158 characters: %q." (len .Params.metaDescription) .Path -}} - {{ end }} - {{if lt (len .Params.metaDescription) 50}} + {{- end -}} + {{- if lt (len .Params.metaDescription) 50 -}} {{- warnf "Page description too short (%d) - should be 50-158 characters: %q." (len .Params.metaDescription) .Path -}} - {{ end }} - {{ with .Params.metaDescription | plainify -}} - {{ . -}} - {{ end }} -{{else if .Description }} - {{ if $recentlyChanged}} - {{if gt (len .Description) 158}} + {{- end -}} + {{- with .Params.metaDescription | plainify -}} + {{- . -}} + {{- end -}} +{{- else if .Description -}} + {{- if $recentlyChanged -}} + {{- if gt (len .Description) 158 -}} {{- warnf "Page description too long (%d) - change it or add a metaDescription with 50-158 characters: %q." (len .Description) .Path -}} - {{ end }} - {{if lt (len .Description) 50}} + {{- end -}} + {{if lt (len .Description) 50 -}} {{- warnf "Page description too short (%d) - change it or add a metaDescription with 50-158 characters: %q.\n This may be a good start:\n%q" (len .Description) .Path (.Summary | plainify) -}} - {{ end }} - {{ end }} - {{ with .Description | plainify -}} - {{ . -}} - {{ end }} -{{ else -}} - {{ if .IsPage -}} - {{ if not .Params.empty_node }} + {{- end -}} + {{- end -}} + {{- with .Description | plainify -}} + {{- . -}} + {{- end -}} +{{- else -}} + {{- if .IsPage -}} + {{- if not .Params.empty_node -}} {{- errorf "No description for page %q" .Path -}} - {{ end -}} - {{ else -}} - {{ with .Site.Params.description | plainify -}} - {{ . -}} - {{ end -}} - {{ end -}} -{{ end -}} + {{- end -}} + {{- else -}} + {{- with .Site.Params.description | plainify -}} + {{- . -}} + {{- end -}} + {{- end -}} +{{- end -}} diff --git a/layouts/partials/scripts.html b/layouts/partials/scripts.html index f6bb798932..88179c1ca3 100644 --- a/layouts/partials/scripts.html +++ b/layouts/partials/scripts.html @@ -38,20 +38,21 @@ {{ $jsModels := resources.Get "js/models.js" }} +{{ if (eq (substr .Site.BaseURL -1) "/" ) }} +{{- $opts := dict +"params" (dict "baseURL" (substr .Site.BaseURL 0 -1 )) +-}} +{{- $jsModels = $jsModels | js.Build $opts -}} +{{ $jsModels := $jsModels | minify }} + +{{ else }} +{{- $opts := dict +"params" (dict "baseURL" .Site.BaseURL) +-}} +{{- $jsModels = $jsModels | js.Build $opts -}} {{ $jsModels := $jsModels | minify }} {{ end }} - - -{{ if .Page.Params.howtojs }} - - - - - -{{ $jsHowtos := resources.Get "js/howtos.js" }} -{{ $jsHowtos := $jsHowtos | minify }} - {{ end }}