Skip to content

Commit

Permalink
feat: Site search using pagefind (#315)
Browse files Browse the repository at this point in the history
* feat: pagefind js api as mvp search

* feat: wip new files for search

* chore: updates

* Prototype search page

* Style search results

* Search pages

* Reorganize search.js

* Add French search page

* No results message

* Add frontmatter to allow excluding some pages from search crawl

* Remove extra styles

* Make search in header bilingual

* Clean up addResult function

* Error handling + remove more content from crawl

* Add web assembly to CSP

* Add nocrawl to 404 page

* Switch to adding results to page in bulk

* Add loader to page when doing logic to render results

* Fix spinner issue when no results found

* PR feedback: Use tokens in search CSS

* Chnage CSS to use token

---------

Co-authored-by: Daine Trinidad <[email protected]>
  • Loading branch information
ethanWallace and daine authored Apr 23, 2024
1 parent fe13968 commit 819a08f
Show file tree
Hide file tree
Showing 45 changed files with 384 additions and 5 deletions.
9 changes: 8 additions & 1 deletion .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const displayTokens = require('./utils/display-tokens');
const markdownAnchor = require('./utils/anchor');
const slugify = require('./utils/slugify');

const { execSync } = require('child_process')

module.exports = function (eleventyConfig) {
// Pass through copies

Expand All @@ -21,6 +23,7 @@ module.exports = function (eleventyConfig) {
eleventyConfig.addPassthroughCopy('./src/styles/prism.css');
eleventyConfig.addPassthroughCopy('./src/images');
eleventyConfig.addPassthroughCopy('./src/scripts/code-showcase.js');
eleventyConfig.addPassthroughCopy('./src/scripts/search.js');
eleventyConfig.addPassthroughCopy('./src/scripts/code-copy.js');
eleventyConfig.addPassthroughCopy('./src/admin/config.yml');
eleventyConfig.addPassthroughCopy('./src/favicon.ico');
Expand Down Expand Up @@ -264,7 +267,7 @@ module.exports = function (eleventyConfig) {
const content = children;

return `
<div class="${margin} b-sm b-default component-preview">
<div class="${margin} b-sm b-default component-preview" id="component-preview">
<p class="container-full font-semibold px-300 py-200 bb-sm b-default bg-light">
${title}
</p>
Expand Down Expand Up @@ -298,6 +301,10 @@ module.exports = function (eleventyConfig) {
});
});

eleventyConfig.on('eleventy.after', () => {
execSync(`npx pagefind --site _site --exclude-selectors "gcds-side-nav, gcds-top-nav, gcds-breadcrumbs, .github-link, .figma-link, h1 > code, .component-preview" --glob \"**/*.html\"`, { encoding: 'utf-8' })
})

return {
pathPrefix: process.env.PATH_PREFIX || '/',
dir: {
Expand Down
2 changes: 1 addition & 1 deletion customHttp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ customHeaders:
report-uri https://csp-report-to.security.cdssandbox.xyz/report;
default-src 'self' https://kit.fontawesome.com/ https://cdn.jsdelivr.net/npm/;
font-src 'self' fonts.gstatic.com https://unpkg.com/[email protected]/;
script-src 'self' www.googletagmanager.com www.google-analytics.com https://kit.fontawesome.com https://cdn.jsdelivr.net/npm/ 'sha256-fipSroUh2te/+TB6wC2Cm4FtjT9kQKTCBmtmSCRONv8=' 'sha256-JDhgsVY611fJnWqj9ydKCOkLlh8t6kvvnJMW3P3GdEg=' 'sha256-Adp/M38ZA9hiuEC+IDM/5MYFu8PJFx0/710dxPMpyYE=';
script-src 'self' 'wasm-unsafe-eval' www.googletagmanager.com www.google-analytics.com https://kit.fontawesome.com https://cdn.jsdelivr.net/npm/ 'sha256-fipSroUh2te/+TB6wC2Cm4FtjT9kQKTCBmtmSCRONv8=' 'sha256-JDhgsVY611fJnWqj9ydKCOkLlh8t6kvvnJMW3P3GdEg=' 'sha256-Adp/M38ZA9hiuEC+IDM/5MYFu8PJFx0/710dxPMpyYE=';
frame-src www.googletagmanager.com www.google-analytics.com https://cds-snc.github.io/;
connect-src 'self' www.googletagmanager.com www.google-analytics.com www.canada.ca;
img-src 'self' data: https: www.w3.org;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@quasibit/eleventy-plugin-sitemap": "^2.1.4",
"chroma-js": "^2.4.2",
"eleventy-plugin-svg-contents": "^0.7.0",
"pagefind": "^1.0.4",
"prettier": "3.2.5",
"prompt-sync": "^4.2.0",
"replace-in-file": "^7.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/404.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ title: Page could not be found | Page introuvable
layout: 'layouts/base.njk'
permalink: '404.html'
eleventyExcludeFromCollections: true
nocrawl: true
locale: en
---

Expand Down
10 changes: 10 additions & 0 deletions src/_data/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
en: {
action: '/en/search',
placeholder: 'GC Design System'
},
fr: {
action: '/fr/recherche',
placeholder: 'Système de design GC'
},
};
7 changes: 6 additions & 1 deletion src/_includes/layouts/base.njk
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@
<link rel="stylesheet" href="{{ '/components/dist/gcds/gcds.css' | url }}">
<script type="module" src="/components/dist/gcds/gcds.esm.js"></script>
<script nomodule src="/components/dist/gcds/gcds.js"></script>

<!-- Pagefind -->
<script src="/pagefind/pagefind.js" type="module"></script>
<script src="{{ '/scripts/search.js' | url }}" type="module"></script>

</head>
<body>
{% include "partials/header.njk" %}

<div class="content-wrapper">
<main id="mc" class="container-xl mx-auto xl:px-0 sm:px-500 px-300 general-layout">
<main id="mc" class="container-xl mx-auto xl:px-0 sm:px-500 px-300 general-layout" {% if not nocrawl %}data-pagefind-body{% endif %}>
{% block content %}{{ content | safe }}{% endblock %}

<gcds-date-modified class="mb-400 font-caption">
Expand Down
5 changes: 5 additions & 0 deletions src/_includes/layouts/home.njk
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<link rel="stylesheet" href="{{ '/components/dist/gcds/gcds.css' | url }}">
<script type="module" src="/components/dist/gcds/gcds.esm.js"></script>
<script nomodule src="/components/dist/gcds/gcds.js"></script>

<!-- Pagefind -->
<script src="/pagefind/pagefind.js" type="module"></script>
<script src="{{ '/scripts/search.js' | url }}" type="module"></script>

</head>
<body>
{% include "partials/header.njk" %}
Expand Down
4 changes: 4 additions & 0 deletions src/_includes/partials/header.njk
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
{% endif %}
{% endfor %}



<gcds-header
lang="{{ locale }}"
skip-to-href="#mc"
lang-href='{{ itemUrl }}'
>
{% include "partials/nav.njk" %}
{% include "partials/breadcrumbs.njk" %}
{% include "partials/search.njk" %}

</gcds-header>
2 changes: 1 addition & 1 deletion src/_includes/partials/helpus.njk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<section class="pt-100 mt-400 mb-600 {% if removeBorder %} {% else %}bt-sm b-dark {% endif %}">
<section data-pagefind-ignore class="pt-100 mt-400 mb-600 {% if removeBorder %} {% else %}bt-sm b-dark {% endif %}">
<h2 class="mt-300 mb-400">
{{ helpus[locale].heading }}
</h2>
Expand Down
4 changes: 4 additions & 0 deletions src/_includes/partials/search.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% if not hidesearch %}
<gcds-search search-id="search" slot="search" placeholder="{{ search[locale].placeholder}}" action="{{ search[locale].action}}">
</gcds-search>
{% endif %}
1 change: 1 addition & 0 deletions src/en/components/alert/code.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ layout: 'layouts/component-documentation.njk'
translationKey: 'alertCode'
tags: ['alertEN', 'code']
date: 'git Last Modified'
nocrawl: true
---

## Code
1 change: 1 addition & 0 deletions src/en/components/alert/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ layout: 'layouts/component-documentation.njk'
translationKey: 'alertDesign'
tags: ['alertEN', 'design']
date: 'git Last Modified'
nocrawl: true
---

## Design
1 change: 1 addition & 0 deletions src/en/components/alert/use-case.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ translationKey: 'alert'
tags: ['alertEN', 'usage']
permalink: /en/components/alert/
date: 'git Last Modified'
nocrawl: true
---

## Usage
1 change: 1 addition & 0 deletions src/en/components/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ eleventyNavigation:
order: 0
translationKey: 'components'
date: 'git Last Modified'
nocrawl: true
cardlist:
type: components
state: published
Expand Down
1 change: 1 addition & 0 deletions src/en/components/data-table/code.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ layout: 'layouts/component-documentation.njk'
translationKey: 'datatableCode'
tags: ['datatableEN', 'code']
# date: "git Last Modified"
nocrawl: true
---

## Build a Data table
Expand Down
1 change: 1 addition & 0 deletions src/en/components/data-table/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ layout: 'layouts/component-documentation.njk'
translationKey: 'datatableDesign'
tags: ['datatableEN', 'design']
# date: "git Last Modified"
nocrawl: true
---

## Data table anatomy
Expand Down
1 change: 1 addition & 0 deletions src/en/components/data-table/use-case.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ translationKey: 'datatable'
tags: ['datatableEN', 'usage']
permalink: /en/components/data-table/
# date: "git Last Modified"
nocrawl: true
---

## Problems component solves
Expand Down
1 change: 1 addition & 0 deletions src/en/components/icon/code.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ layout: 'layouts/component-documentation.njk'
translationKey: 'iconCode'
tags: ['iconEN', 'code']
date: 'git Last Modified'
nocrawl: true
---

## Code
1 change: 1 addition & 0 deletions src/en/components/icon/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ layout: 'layouts/component-documentation.njk'
translationKey: 'iconDesign'
tags: ['iconEN', 'design']
date: 'git Last Modified'
nocrawl: true
---

## Design
1 change: 1 addition & 0 deletions src/en/components/icon/use-case.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ translationKey: 'icon'
tags: ['iconEN', 'usage']
permalink: /en/components/icon/
date: 'git Last Modified'
nocrawl: true
---

## Usage
1 change: 1 addition & 0 deletions src/en/components/image/code.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ layout: 'layouts/component-documentation.njk'
translationKey: 'imageCode'
tags: ['imageEN', 'code']
# date: "git Last Modified"
nocrawl: true
---

## Build a Image
Expand Down
1 change: 1 addition & 0 deletions src/en/components/image/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ layout: 'layouts/component-documentation.njk'
translationKey: 'imageDesign'
tags: ['imageEN', 'design']
# date: "git Last Modified"
nocrawl: true
---

## Image anatomy
Expand Down
1 change: 1 addition & 0 deletions src/en/components/image/use-case.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ translationKey: 'image'
tags: ['imageEN', 'usage']
permalink: /en/components/image/
# date: "git Last Modified"
nocrawl: true
---

## Problems component solves
Expand Down
1 change: 1 addition & 0 deletions src/en/demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
title: Demo
translationKey: demo
layout: 'layouts/demo.njk'
nocrawl: true
---
34 changes: 34 additions & 0 deletions src/en/search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
title: Search
translationKey: searchresults
layout: 'layouts/base.njk'
hidesearch: true
nocrawl: true
date: 'git Last Modified'
eleventyNavigation:
key: searchresultsEN
title: Search
locale: en
order: 5
hideMain: true
---

# Search results

<gcds-search
action="/en/search"
search-id="ds-search"
placeholder="GC Design System"
id="searchbar"
>
</gcds-search>
<gcds-text size="caption">
Don’t include personal information (telephone, email, SIN, financial, medical, or work details).
</gcds-text>

<div id="results-count"></div>

<div id="results"></div>

<div id="pagination"></div>
1 change: 1 addition & 0 deletions src/fr/composants/alerte/cas-dutilisation.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ translationKey: 'alert'
tags: ['alertFR', 'usage']
permalink: /fr/composants/alerte/
date: 'git Last Modified'
nocrawl: true
---

## Usage
1 change: 1 addition & 0 deletions src/fr/composants/alerte/code.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ layout: 'layouts/component-documentation.njk'
translationKey: 'alertCode'
tags: ['alertFR', 'code']
date: 'git Last Modified'
nocrawl: true
---

## Code
1 change: 1 addition & 0 deletions src/fr/composants/alerte/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ layout: 'layouts/component-documentation.njk'
translationKey: 'alertDesign'
tags: ['alertFR', 'design']
date: 'git Last Modified'
nocrawl: true
---

## Design
1 change: 1 addition & 0 deletions src/fr/composants/composants.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ eleventyNavigation:
order: 0
translationKey: 'components'
date: 'git Last Modified'
nocrawl: true
cardlist:
type: components
state: published
Expand Down
1 change: 1 addition & 0 deletions src/fr/composants/icone/cas-dutilisation.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ translationKey: 'icon'
tags: ['iconFR', 'usage']
permalink: /fr/composants/icone/
date: 'git Last Modified'
nocrawl: true
---

## Usage
1 change: 1 addition & 0 deletions src/fr/composants/icone/code.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ layout: 'layouts/component-documentation.njk'
translationKey: 'iconCode'
tags: ['iconFR', 'code']
date: 'git Last Modified'
nocrawl: true
---

## Code
1 change: 1 addition & 0 deletions src/fr/composants/icone/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ layout: 'layouts/component-documentation.njk'
translationKey: 'iconDesign'
tags: ['iconFR', 'design']
date: 'git Last Modified'
nocrawl: true
---

## Design
1 change: 1 addition & 0 deletions src/fr/composants/image/cas-dutilisation.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ translationKey: 'image'
tags: ['imageFR', 'usage']
permalink: /fr/composants/image/
# date: "git Last Modified"
nocrawl: true
---

## Problems component solves
Expand Down
1 change: 1 addition & 0 deletions src/fr/composants/image/code.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ layout: 'layouts/component-documentation.njk'
translationKey: 'imageCode'
tags: ['imageFR', 'code']
# date: "git Last Modified"
nocrawl: true
---

## Créer une case à Image
Expand Down
1 change: 1 addition & 0 deletions src/fr/composants/image/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ layout: 'layouts/component-documentation.njk'
translationKey: 'imageDesign'
tags: ['imageFR', 'design']
# date: "git Last Modified"
nocrawl: true
---

## Structure de la Image
Expand Down
1 change: 1 addition & 0 deletions src/fr/composants/tableau-de-donnees/cas-dutilisation.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ translationKey: 'datatable'
tags: ['datatableFR', 'usage']
permalink: /fr/composants/tableau-de-donnees/
# date: "git Last Modified"
nocrawl: true
---

## Problems component solves
Expand Down
1 change: 1 addition & 0 deletions src/fr/composants/tableau-de-donnees/code.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ layout: 'layouts/component-documentation.njk'
translationKey: 'datatableCode'
tags: ['datatableFR', 'code']
# date: "git Last Modified"
nocrawl: true
---

## Créer une case à Tableau de données
Expand Down
1 change: 1 addition & 0 deletions src/fr/composants/tableau-de-donnees/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ layout: 'layouts/component-documentation.njk'
translationKey: 'datatableDesign'
tags: ['datatableFR', 'design']
# date: "git Last Modified"
nocrawl: true
---

## Structure de la Tableau de données
Expand Down
1 change: 1 addition & 0 deletions src/fr/demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
title: Demo
translationKey: demo
layout: 'layouts/demo.njk'
nocrawl: true
---
Loading

0 comments on commit 819a08f

Please sign in to comment.