From 514f1be060adfcfe8fd7a07ac7fb220dcd67e9c2 Mon Sep 17 00:00:00 2001 From: Ole Eskild Steensen Date: Thu, 26 Jan 2023 09:08:13 +0100 Subject: [PATCH] Escape "\" in tags, as they are invalid json --- .eleventy.js | 9 +++++++++ src/site/lunr.njk | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.eleventy.js b/.eleventy.js index d53e6980c..e62cb2597 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -289,6 +289,15 @@ module.exports = function (eleventyConfig) { return JSON.stringify(variable) || '""'; }); + eleventyConfig.addFilter('validJson', function (variable) { + if(Array.isArray((variable))){ + return variable.map(x=>x.replaceAll("\\", "\\\\")).join(","); + }else if(typeof(variable) === 'string'){ + return variable.replaceAll("\\", "\\\\"); + } + return variable; + }); + return { dir: { input: "src/site", diff --git a/src/site/lunr.njk b/src/site/lunr.njk index 9b1f14d7d..8e79500f0 100644 --- a/src/site/lunr.njk +++ b/src/site/lunr.njk @@ -9,6 +9,6 @@ eleventyExcludeFromCollections: true "date":"{{ post.date }}", "url":"{{ post.url }}", "content": {{ post.templateContent | striptags(true) | link | jsonify | safe }}, - "tags": [{{post.templateContent | link | searchableTags | safe }} {% if post.data.tags %}{% for tag in post.data.tags %}"{{tag}}"{% if not loop.last %},{% endif %}{% endfor %}{% endif %}] + "tags": [{{post.templateContent | link | searchableTags | safe }} {% if post.data.tags %}{% for tag in post.data.tags %}"{{tag|validJson}}"{% if not loop.last %},{% endif %}{% endfor %}{% endif %}] }{% if not loop.last %},{% endif %} {% endfor %}]