diff --git a/src/en/news/blog/search-output.11ty.js b/src/en/news/blog/search-output.11ty.js new file mode 100644 index 000000000..57e5b6e4a --- /dev/null +++ b/src/en/news/blog/search-output.11ty.js @@ -0,0 +1,27 @@ +const cleanCardContent = require('../../../_11ty/filters/cleanCardContent'); +const truncate = require('../../../_11ty/filters/truncate'); + +module.exports = class { + data() { + return { + layout: false, + permalink: data => `/${data.locale}/news/blog/search-output.json`, + eleventyExcludeFromCollections: true, + }; + } + + render({ collections, locale }) { + const collection = collections[`${locale}-blog-post`] || []; + + const articles = collection.map(item => ({ + image: item.data.image || '', + title: item.data.title, + author: item.data.author, + date: item.data.date, + url: item.url, + content: truncate(cleanCardContent(item.templateContent)), + })); + + return JSON.stringify(articles); + } +}; diff --git a/src/en/news/blog/search-output.html b/src/en/news/blog/search-output.html deleted file mode 100644 index 6a702d942..000000000 --- a/src/en/news/blog/search-output.html +++ /dev/null @@ -1,10 +0,0 @@ ---- -layout: null -permalink: '/{{ locale }}/news/blog/search-output.json' -override:tags: [] -eleventyExcludeFromCollections: true ---- - -[ {% for item in collections['en-blog-post'] %} { "image":"{{item.data.image}}", "title":"{{item.data.title}}", -"author":"{{item.data.author}}", "date":"{{item.data.date}}", "url":"{{item.url}}", "content":"{{ item.templateContent | cleanCardContent | -truncate }}" }{% if loop.last != true %},{% endif %} {% endfor %} ] diff --git a/src/en/news/blog/search-raw.11ty.js b/src/en/news/blog/search-raw.11ty.js new file mode 100644 index 000000000..59f802f4d --- /dev/null +++ b/src/en/news/blog/search-raw.11ty.js @@ -0,0 +1,23 @@ +const cleanSearchRaw = require('../../../_11ty/filters/cleanSearchRaw'); + +module.exports = class { + data() { + return { + layout: false, + permalink: data => `/${data.locale}/news/blog/search-raw.json`, + eleventyExcludeFromCollections: true, + }; + } + + render({ collections, locale }) { + const collection = collections[`${locale}-blog-post`] || []; + + const articles = collection.map(item => ({ + title: item.data.title, + author: item.data.author, + content: cleanSearchRaw(item.templateContent), + })); + + return JSON.stringify(articles); + } +}; diff --git a/src/en/news/blog/search-raw.html b/src/en/news/blog/search-raw.html deleted file mode 100644 index f98a8c922..000000000 --- a/src/en/news/blog/search-raw.html +++ /dev/null @@ -1,9 +0,0 @@ ---- -layout: null -permalink: '/{{ locale }}/news/blog/search-raw.json' -override:tags: [] -eleventyExcludeFromCollections: true ---- - -[ {% for item in collections['en-blog-post'] %} { "title":"{{ item.data.title }}", "author":"{{ item.data.author }}", "content":"{{ -item.templateContent | cleanSearchRaw }}" }{% if loop.last != true %},{% endif %} {% endfor %} ] diff --git a/src/js/search-output.js b/src/js/search-output.js index 154595f08..b20d75102 100644 --- a/src/js/search-output.js +++ b/src/js/search-output.js @@ -2,6 +2,15 @@ import lunr from 'lunr'; import articleCard from '../_11ty/shortcodes/ArticleCard.js'; import translations from '../_data/i18n'; +function escapeHtml(value) { + return String(value) + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); +} + const SearchOutput = { init: () => { const urlParts = window.location.pathname.split('/') || []; @@ -15,7 +24,7 @@ const SearchOutput = { const searchresultsContainer = document.getElementById('search-results'); const queryString = window.location.search; const urlParams = new URLSearchParams(queryString); - const query = urlParams.get('q'); + const query = urlParams.get('q') || ''; async function initSearchIndex() { if (!urlParams.has('q')) return; @@ -25,11 +34,7 @@ const SearchOutput = { const [searchIndex, searchOutput] = await Promise.all( searchDataUrls.map(url => - fetch(url, { - method: 'GET', - credentials: 'include', - mode: 'no-cors', - }).then(res => res.json()) + fetch(url, { method: 'GET' }).then(res => res.json()) ) ); const lunrIndex = lunr.Index.load(searchIndex); @@ -65,13 +70,14 @@ const SearchOutput = { blog_search_no_results[urlLocale] || 'No results for'; const searchedForString = blog_searched_for[urlLocale] || 'You searched for'; + const safeQuery = escapeHtml(query); if (!searchresultsContainer) return; if (!results.length) { - searchResultsHtml = `

${noResultsString} “${query}”

`; + searchResultsHtml = `

${noResultsString} “${safeQuery}”

`; } else { - searchResultsHtml = `

${searchedForString} “${query}”

+ searchResultsHtml = `

${searchedForString} “${safeQuery}”