Skip to content

Commit

Permalink
Escape "\" in tags, as they are invalid json
Browse files Browse the repository at this point in the history
  • Loading branch information
Ole Eskild Steensen committed Jan 26, 2023
1 parent 6c8f6a5 commit 514f1be
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/site/lunr.njk
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}]

0 comments on commit 514f1be

Please sign in to comment.