Skip to content

Commit

Permalink
v1.2.4 Now adding the page title to the capture
Browse files Browse the repository at this point in the history
This will show at the bottom of the page above the source and in the
frontmatter as source-page-title
  • Loading branch information
davidCburke committed Sep 22, 2024
1 parent a276546 commit 979c34a
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 25 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.3';
const version = '1.2.4';
// 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.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)

## 1.2.3

- Added default tags to the configure page. The tags text box on the capture page will be automatically populated with the default tags defined here. Multiple tags, separated by spaces, can be added to the default tags. The tags can be entered with or without a leading hash. For example, this is a valid entry: tag1 tag2 #tag3
Expand Down
16 changes: 9 additions & 7 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.tags, message.saveMetadataAsFrontmatter);
convertToMarkdown(message.data, message.url, message.title, message.pageTitle, message.tags, message.saveMetadataAsFrontmatter);
break;
default:
console.warn(`Unexpected message type received: '${message.type}'.`);
Expand All @@ -16,14 +16,15 @@ async function handleMessages(message) {
}

/* Uses TurndownService to create markdown from a HTML string and user entered tags */
function convertToMarkdown(htmlString, url, title, tags, saveMetadataAsFrontmatter) {
function convertToMarkdown(htmlString, url, title, pageTitle, tags, saveMetadataAsFrontmatter) {
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-url: ' + url + '\n';
frontmatter += 'created-date: ' + getDateStamp(new Date());
}
Expand All @@ -50,13 +51,14 @@ function convertToMarkdown(htmlString, url, title, tags, saveMetadataAsFrontmatt
tagsMarkdown += '</p>'
}
}
if(htmlString != null) { //If there is HTML then create the markdown using the tags and creating a line for the source URL of the capture
markdown = turndownService.turndown(((saveMetadataAsFrontmatter)?'':tagsMarkdown) + htmlString + '<p>source: urlPlaceholder</p>');
} else { //If there is no HTML then just capture the tab URL and any tags
markdown = turndownService.turndown(((saveMetadataAsFrontmatter)?'':tagsMarkdown) + '<p>urlPlaceholder</p>');
let markdownText = (saveMetadataAsFrontmatter ? '' : tagsMarkdown);
if (htmlString != null) { //If there is HTML then create the markdown using the tags and creating a line for the source URL of the capture
markdownText += htmlString;
}
markdown = turndownService.turndown(markdownText + '<p>title: pageTitlePlaceHolder<br>source: urlPlaceholder</p>');
//The turndown service escapes any markdown characters in the URL and breaks the link so add the url after markdown coversion
markdown = markdown.replace("urlPlaceholder", url);
markdown = markdown.replace("pageTitlePlaceHolder", pageTitle);
//Add frontmatter if wanted
if(saveMetadataAsFrontmatter) {
markdown = frontmatter + '\n' + '---' + '\n\n' + markdown;
Expand All @@ -82,4 +84,4 @@ function sendToServiceWorker(type, data, title) {
data,
title
});
}
}
13 changes: 9 additions & 4 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, title, tags, saveMetadataAsFrontmatter) {
async function sendMessageToOffscreenDocument(type, data, url, pageTitle, title, tags, saveMetadataAsFrontmatter) {
if (!(await hasDocument())) {
await chrome.offscreen.createDocument({
url: OFFSCREEN_DOCUMENT_PATH,
Expand All @@ -89,6 +89,7 @@ async function sendMessageToOffscreenDocument(type, data, url, title, tags, save
target: 'offscreen',
data,
url,
pageTitle,
title,
tags,
saveMetadataAsFrontmatter
Expand Down Expand Up @@ -122,11 +123,14 @@ async function captureTab(title, tags, appendPageTitle, saveMetadataAsFrontmatte
let queryOptions = { active: true, lastFocusedWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
const url = tab.url;

//Get the title of the web page from the tab
let pageTitle = await getTitleFromTab(tab.id);

if(appendPageTitle) {
//Get the title of the web page from the tab
let pageTitle = await getTitleFromTab(tab.id);
title += ' (' + pageTitle + ')';
}

const invalidCharactersRegex = /[^a-zA-Z0-9\-_\s\(\):]/g;
title = title.replace(invalidCharactersRegex,'_');
chrome.storage.sync.get(["maxTitleLength"], (items) => {
Expand All @@ -141,6 +145,7 @@ async function captureTab(title, tags, appendPageTitle, saveMetadataAsFrontmatte
'convert-to-markdown',
text,
url,
pageTitle,
title,
tags,
saveMetadataAsFrontmatter
Expand Down Expand Up @@ -210,4 +215,4 @@ function sendCaptureToEndpoint(markdown, title) {
/* Get the current date stamp */
function getDateStamp(date) {
return date.getFullYear().toString() + '-' + (date.getMonth() + 1).toString().padStart(2,'0') + '-' + date.getDate().toString().padStart(2,'0');
}
}
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.3",
"version": "1.2.4",
"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
16 changes: 9 additions & 7 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.tags, message.saveMetadataAsFrontmatter);
convertToMarkdown(message.data, message.url, message.title, message.pageTitle, message.tags, message.saveMetadataAsFrontmatter);
break;
default:
console.warn(`Unexpected message type received: '${message.type}'.`);
Expand All @@ -16,14 +16,15 @@ async function handleMessages(message) {
}

/* Uses TurndownService to create markdown from a HTML string and user entered tags */
function convertToMarkdown(htmlString, url, title, tags, saveMetadataAsFrontmatter) {
function convertToMarkdown(htmlString, url, title, pageTitle, tags, saveMetadataAsFrontmatter) {
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-url: ' + url + '\n';
frontmatter += 'created-date: ' + getDateStamp(new Date());
}
Expand All @@ -50,13 +51,14 @@ function convertToMarkdown(htmlString, url, title, tags, saveMetadataAsFrontmatt
tagsMarkdown += '</p>'
}
}
if(htmlString != null) { //If there is HTML then create the markdown using the tags and creating a line for the source URL of the capture
markdown = turndownService.turndown(((saveMetadataAsFrontmatter)?'':tagsMarkdown) + htmlString + '<p>source: urlPlaceholder</p>');
} else { //If there is no HTML then just capture the tab URL and any tags
markdown = turndownService.turndown(((saveMetadataAsFrontmatter)?'':tagsMarkdown) + '<p>urlPlaceholder</p>');
let markdownText = (saveMetadataAsFrontmatter ? '' : tagsMarkdown);
if (htmlString != null) { //If there is HTML then create the markdown using the tags and creating a line for the source URL of the capture
markdownText += htmlString;
}
markdown = turndownService.turndown(markdownText + '<p>title: pageTitlePlaceHolder<br>source: urlPlaceholder</p>');
//The turndown service escapes any markdown characters in the URL and breaks the link so add the url after markdown coversion
markdown = markdown.replace("urlPlaceholder", url);
markdown = markdown.replace("pageTitlePlaceHolder", pageTitle);
//Add frontmatter if wanted
if(saveMetadataAsFrontmatter) {
markdown = frontmatter + '\n' + '---' + '\n\n' + markdown;
Expand All @@ -82,4 +84,4 @@ function sendToServiceWorker(type, data, title) {
data,
title
});
}
}
13 changes: 9 additions & 4 deletions dist/firefox/js/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ async function getTitleFromTab(tabId) {
}

/* Use an offscreen document to parse the captured DOM */
function sendMessageToOffscreenDocument(type, data, url, title, tags, saveMetadataAsFrontmatter) {
function sendMessageToOffscreenDocument(type, data, url, pageTitle, title, tags, saveMetadataAsFrontmatter) {
browser.runtime.sendMessage({
type,
target: 'offscreen',
data,
url,
pageTitle,
title,
tags,
saveMetadataAsFrontmatter
Expand Down Expand Up @@ -113,11 +114,14 @@ async function captureTab(title, tags, appendPageTitle, saveMetadataAsFrontmatte
let queryOptions = { active: true, lastFocusedWindow: true };
let [tab] = await browser.tabs.query(queryOptions);
const url = tab.url;

//Get the title of the web page from the tab
let pageTitle = await getTitleFromTab(tab.id);

if(appendPageTitle) {
//Get the title of the web page from the tab
let pageTitle = await getTitleFromTab(tab.id);
title += ' (' + pageTitle + ')';
}

const invalidCharactersRegex = /[^a-zA-Z0-9\-_\s\(\):]/g;
title = title.replace(invalidCharactersRegex,'_');
browser.storage.sync.get(["maxTitleLength"], (items) => {
Expand All @@ -132,6 +136,7 @@ async function captureTab(title, tags, appendPageTitle, saveMetadataAsFrontmatte
'convert-to-markdown',
text,
url,
pageTitle,
title,
tags,
saveMetadataAsFrontmatter
Expand Down Expand Up @@ -182,4 +187,4 @@ function sendCaptureToEndpoint(markdown, title) {
/* Get the current date stamp */
function getDateStamp(date) {
return date.getFullYear().toString() + '-' + (date.getMonth() + 1).toString().padStart(2,'0') + '-' + date.getDate().toString().padStart(2,'0');
}
}
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.3",
"version": "1.2.4",
"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

0 comments on commit 979c34a

Please sign in to comment.