Skip to content

Commit

Permalink
Fixed invalid characters in frontmatter YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
davidCburke authored and davecburke committed Dec 20, 2024
1 parent 6da491e commit 4029d03
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function(grunt) {
const version = '1.2.4';
const version = '1.2.5';
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ SilverBullet Clipper uses the following libraries:

# Version History

## 1.2.5

- Fixed issues caused by invalid YAML characters in Frontmatter

## 1.2.4

- Now adding the page title to the capture. This will show at the bottom of the page above the source and in the frontmatter as source-page-title. Thanks [dklawran](https://github.com/dklawren)
Expand Down
23 changes: 19 additions & 4 deletions dist/chrome/js/offscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ async function handleMessages(message) {
}
switch (message.type) {
case 'convert-to-markdown':
convertToMarkdown(message.data, message.url, message.title, message.pageTitle, message.tags, message.saveMetadataAsFrontmatter);
convertToMarkdown(message.data, message.url, message.title, message.pageTitle, message.tags, message.saveMetadataAsFrontmatter, message.sourceTitle);
break;
default:
console.warn(`Unexpected message type received: '${message.type}'.`);
Expand All @@ -16,15 +16,15 @@ async function handleMessages(message) {
}

/* Uses TurndownService to create markdown from a HTML string and user entered tags */
function convertToMarkdown(htmlString, url, title, pageTitle, tags, saveMetadataAsFrontmatter) {
function convertToMarkdown(htmlString, url, title, pageTitle, tags, saveMetadataAsFrontmatter, sourceTitle) {
var turndownService = new TurndownService();
var markdown;
var tagsMarkdown = '';
var frontmatter = '';
if(saveMetadataAsFrontmatter) {
frontmatter = '---\n';
frontmatter += 'source-title: ' + title.replaceAll(':','-') + '\n';
frontmatter += 'source-page-title: ' + pageTitle + '\n';
frontmatter += 'source-title: ' + sanitizeYamlString(sourceTitle) + '\n';
frontmatter += 'source-page-title: ' + sanitizeYamlString(pageTitle) + '\n';
frontmatter += 'source-url: ' + url + '\n';
frontmatter += 'created-date: ' + getDateStamp(new Date());
}
Expand Down Expand Up @@ -85,3 +85,18 @@ function sendToServiceWorker(type, data, title) {
title
});
}

function sanitizeYamlString(string) {
const specialChars = [':', '-', '?', '{', '}', '[', ']', ',', '#', '&', '*', '!', '|', '>', "'", '"', '%', '@', '`'];

// Check if the string contains any special YAML characters
if (specialChars.some(char => string.includes(char))) {
// Use double quotes if the string contains single quotes
if (string.includes("'")) {
return `"${string}"`;
}
// Otherwise, use single quotes
return `'${string}'`;
}
return string;
}
9 changes: 6 additions & 3 deletions dist/chrome/js/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async function getTitleFromTab(tabId) {
}

/* Use an offscreen document to parse the captured DOM */
async function sendMessageToOffscreenDocument(type, data, url, pageTitle, title, tags, saveMetadataAsFrontmatter) {
async function sendMessageToOffscreenDocument(type, data, url, pageTitle, title, tags, saveMetadataAsFrontmatter, sourceTitle) {
if (!(await hasDocument())) {
await chrome.offscreen.createDocument({
url: OFFSCREEN_DOCUMENT_PATH,
Expand All @@ -92,7 +92,8 @@ async function sendMessageToOffscreenDocument(type, data, url, pageTitle, title,
pageTitle,
title,
tags,
saveMetadataAsFrontmatter
saveMetadataAsFrontmatter,
sourceTitle
});
}

Expand Down Expand Up @@ -131,6 +132,7 @@ async function captureTab(title, tags, appendPageTitle, saveMetadataAsFrontmatte
title += ' (' + pageTitle + ')';
}

let sourceTitle = title;
const invalidCharactersRegex = /[^a-zA-Z0-9\-_\s\(\):]/g;
title = title.replace(invalidCharactersRegex,'_');
chrome.storage.sync.get(["maxTitleLength"], (items) => {
Expand All @@ -148,7 +150,8 @@ async function captureTab(title, tags, appendPageTitle, saveMetadataAsFrontmatte
pageTitle,
title,
tags,
saveMetadataAsFrontmatter
saveMetadataAsFrontmatter,
sourceTitle
);
}

Expand Down
2 changes: 1 addition & 1 deletion dist/chrome/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "SilverBullet Clipper",
"version": "1.2.4",
"version": "1.2.5",
"manifest_version": 3,
"description": "SilverBullet Clipper captures a web page URL or selected content, converts it to markdown and saves it in the SilverBullet Inbox",
"icons": {
Expand Down
23 changes: 19 additions & 4 deletions dist/firefox/js/offscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ async function handleMessages(message) {
}
switch (message.type) {
case 'convert-to-markdown':
convertToMarkdown(message.data, message.url, message.title, message.pageTitle, message.tags, message.saveMetadataAsFrontmatter);
convertToMarkdown(message.data, message.url, message.title, message.pageTitle, message.tags, message.saveMetadataAsFrontmatter, message.sourceTitle);
break;
default:
console.warn(`Unexpected message type received: '${message.type}'.`);
Expand All @@ -16,15 +16,15 @@ async function handleMessages(message) {
}

/* Uses TurndownService to create markdown from a HTML string and user entered tags */
function convertToMarkdown(htmlString, url, title, pageTitle, tags, saveMetadataAsFrontmatter) {
function convertToMarkdown(htmlString, url, title, pageTitle, tags, saveMetadataAsFrontmatter, sourceTitle) {
var turndownService = new TurndownService();
var markdown;
var tagsMarkdown = '';
var frontmatter = '';
if(saveMetadataAsFrontmatter) {
frontmatter = '---\n';
frontmatter += 'source-title: ' + title.replaceAll(':','-') + '\n';
frontmatter += 'source-page-title: ' + pageTitle + '\n';
frontmatter += 'source-title: ' + sanitizeYamlString(sourceTitle) + '\n';
frontmatter += 'source-page-title: ' + sanitizeYamlString(pageTitle) + '\n';
frontmatter += 'source-url: ' + url + '\n';
frontmatter += 'created-date: ' + getDateStamp(new Date());
}
Expand Down Expand Up @@ -85,3 +85,18 @@ function sendToServiceWorker(type, data, title) {
title
});
}

function sanitizeYamlString(string) {
const specialChars = [':', '-', '?', '{', '}', '[', ']', ',', '#', '&', '*', '!', '|', '>', "'", '"', '%', '@', '`'];

// Check if the string contains any special YAML characters
if (specialChars.some(char => string.includes(char))) {
// Use double quotes if the string contains single quotes
if (string.includes("'")) {
return `"${string}"`;
}
// Otherwise, use single quotes
return `'${string}'`;
}
return string;
}
9 changes: 6 additions & 3 deletions dist/firefox/js/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async function getTitleFromTab(tabId) {
}

/* Use an offscreen document to parse the captured DOM */
function sendMessageToOffscreenDocument(type, data, url, pageTitle, title, tags, saveMetadataAsFrontmatter) {
function sendMessageToOffscreenDocument(type, data, url, pageTitle, title, tags, saveMetadataAsFrontmatter, sourceTitle) {
browser.runtime.sendMessage({
type,
target: 'offscreen',
Expand All @@ -82,7 +82,8 @@ function sendMessageToOffscreenDocument(type, data, url, pageTitle, title, tags,
pageTitle,
title,
tags,
saveMetadataAsFrontmatter
saveMetadataAsFrontmatter,
sourceTitle
});
}

Expand Down Expand Up @@ -122,6 +123,7 @@ async function captureTab(title, tags, appendPageTitle, saveMetadataAsFrontmatte
title += ' (' + pageTitle + ')';
}

let sourceTitle = title;
const invalidCharactersRegex = /[^a-zA-Z0-9\-_\s\(\):]/g;
title = title.replace(invalidCharactersRegex,'_');
browser.storage.sync.get(["maxTitleLength"], (items) => {
Expand All @@ -139,7 +141,8 @@ async function captureTab(title, tags, appendPageTitle, saveMetadataAsFrontmatte
pageTitle,
title,
tags,
saveMetadataAsFrontmatter
saveMetadataAsFrontmatter,
sourceTitle
);
}

Expand Down
2 changes: 1 addition & 1 deletion dist/firefox/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "SilverBullet Clipper",
"version": "1.2.4",
"version": "1.2.5",
"manifest_version": 2,
"description": "SilverBullet Clipper captures a web page URL or selected content, converts it to markdown and saves it in the SilverBullet Inbox",
"icons": {
Expand Down
23 changes: 19 additions & 4 deletions src/js/offscreen.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ async function handleMessages(message) {
}
switch (message.type) {
case 'convert-to-markdown':
convertToMarkdown(message.data, message.url, message.title, message.pageTitle, message.tags, message.saveMetadataAsFrontmatter);
convertToMarkdown(message.data, message.url, message.title, message.pageTitle, message.tags, message.saveMetadataAsFrontmatter, message.sourceTitle);
break;
default:
console.warn(`Unexpected message type received: '${message.type}'.`);
Expand All @@ -16,15 +16,15 @@ async function handleMessages(message) {
}

/* Uses TurndownService to create markdown from a HTML string and user entered tags */
function convertToMarkdown(htmlString, url, title, pageTitle, tags, saveMetadataAsFrontmatter) {
function convertToMarkdown(htmlString, url, title, pageTitle, tags, saveMetadataAsFrontmatter, sourceTitle) {
var turndownService = new TurndownService();
var markdown;
var tagsMarkdown = '';
var frontmatter = '';
if(saveMetadataAsFrontmatter) {
frontmatter = '---\n';
frontmatter += 'source-title: ' + title.replaceAll(':','-') + '\n';
frontmatter += 'source-page-title: ' + pageTitle + '\n';
frontmatter += 'source-title: ' + sanitizeYamlString(sourceTitle) + '\n';
frontmatter += 'source-page-title: ' + sanitizeYamlString(pageTitle) + '\n';
frontmatter += 'source-url: ' + url + '\n';
frontmatter += 'created-date: ' + getDateStamp(new Date());
}
Expand Down Expand Up @@ -85,3 +85,18 @@ function sendToServiceWorker(type, data, title) {
title
});
}

function sanitizeYamlString(string) {
const specialChars = [':', '-', '?', '{', '}', '[', ']', ',', '#', '&', '*', '!', '|', '>', "'", '"', '%', '@', '`'];

// Check if the string contains any special YAML characters
if (specialChars.some(char => string.includes(char))) {
// Use double quotes if the string contains single quotes
if (string.includes("'")) {
return `"${string}"`;
}
// Otherwise, use single quotes
return `'${string}'`;
}
return string;
}
14 changes: 9 additions & 5 deletions src/js/service-worker.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async function getTitleFromTab(tabId) {
}

/* Use an offscreen document to parse the captured DOM */
async function sendMessageToOffscreenDocument(type, data, url, pageTitle, title, tags, saveMetadataAsFrontmatter) {
async function sendMessageToOffscreenDocument(type, data, url, pageTitle, title, tags, saveMetadataAsFrontmatter, sourceTitle) {
if (!(await hasDocument())) {
await chrome.offscreen.createDocument({
url: OFFSCREEN_DOCUMENT_PATH,
Expand All @@ -92,7 +92,8 @@ async function sendMessageToOffscreenDocument(type, data, url, pageTitle, title,
pageTitle,
title,
tags,
saveMetadataAsFrontmatter
saveMetadataAsFrontmatter,
sourceTitle
});
}<% } %><% if (taskName === 'firefox') { %>async function getTextFromSelection(tabId) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -167,7 +168,7 @@ async function getTitleFromTab(tabId) {
}

/* Use an offscreen document to parse the captured DOM */
function sendMessageToOffscreenDocument(type, data, url, pageTitle, title, tags, saveMetadataAsFrontmatter) {
function sendMessageToOffscreenDocument(type, data, url, pageTitle, title, tags, saveMetadataAsFrontmatter, sourceTitle) {
browser.runtime.sendMessage({
type,
target: 'offscreen',
Expand All @@ -176,7 +177,8 @@ function sendMessageToOffscreenDocument(type, data, url, pageTitle, title, tags,
pageTitle,
title,
tags,
saveMetadataAsFrontmatter
saveMetadataAsFrontmatter,
sourceTitle
});
}
<% } %>
Expand Down Expand Up @@ -216,6 +218,7 @@ async function captureTab(title, tags, appendPageTitle, saveMetadataAsFrontmatte
title += ' (' + pageTitle + ')';
}

let sourceTitle = title;
const invalidCharactersRegex = /[^a-zA-Z0-9\-_\s\(\):]/g;
title = title.replace(invalidCharactersRegex,'_');
<%= runTime %>.storage.sync.get(["maxTitleLength"], (items) => {
Expand All @@ -233,7 +236,8 @@ async function captureTab(title, tags, appendPageTitle, saveMetadataAsFrontmatte
pageTitle,
title,
tags,
saveMetadataAsFrontmatter
saveMetadataAsFrontmatter,
sourceTitle
);
}

Expand Down

0 comments on commit 4029d03

Please sign in to comment.