Escape "\" in tags, as they are invalid json

This commit is contained in:
Ole Eskild Steensen 2023-01-26 09:08:13 +01:00
parent 6c8f6a5ebe
commit 514f1be060
2 changed files with 10 additions and 1 deletions

View File

@ -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",

View File

@ -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 %}]