Skip to content

Commit

Permalink
Keep source formatting in blockquotes
Browse files Browse the repository at this point in the history
Closes #37, closes #36, closes #39.
  • Loading branch information
wheelercj committed Jun 29, 2024
1 parent 135fb4e commit ef68b2a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
15 changes: 8 additions & 7 deletions src/htmlSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ export async function createMd(title, url, selection) {
const selectionFormat = await getSetting('selectionFormat');
switch (selectionFormat) {
case 'source with link':
return await getSourceFormatMdWithLink(title, url, selection, selectedText);
return await getSourceFormatMdWithLink(title, url, selection, selectedText) + '\n';
case 'source':
return await getSourceFormatMd(selection, selectedText);
return await getSourceFormatMd(selection, selectedText) + '\n';
case 'blockquote with link':
return await md.createBlockquote(selectedText, title, url) + '\n';
const body = await getSourceFormatMd(selection, selectedText);
return await md.createBlockquote(body, title, url) + '\n';
case 'link with selection as title':
selectedText = selectedText.replaceAll('\r\n', ' ').replaceAll('\n', ' ');
return await md.createLink(selectedText, url);
Expand All @@ -66,9 +67,9 @@ async function getSourceFormatMdWithLink(title, url, selection, selectedText) {
const alert = await md.createAlert('note', `from ${link}`);
const html = await getSelectionHtml(selection);
if (html === null) {
return alert + '\n\n' + selectedText + '\n';
return alert + '\n\n' + selectedText;
} else {
return alert + '\n\n' + await md.htmlToMarkdown(html) + '\n';
return alert + '\n\n' + await md.htmlToMarkdown(html);
}
}

Expand All @@ -83,9 +84,9 @@ async function getSourceFormatMdWithLink(title, url, selection, selectedText) {
async function getSourceFormatMd(selection, selectedText) {
const html = await getSelectionHtml(selection);
if (html === null) {
return selectedText + '\n';
return selectedText;
} else {
return await md.htmlToMarkdown(html) + '\n';
return await md.htmlToMarkdown(html);
}
}

Expand Down
14 changes: 4 additions & 10 deletions src/md.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,15 @@ export async function createAlert(type, text) {

/**
* createBlockquote creates a markdown blockquote with a link at the end.
* @param {string} text - the text of the blockquote. Many markdown patterns are
* escaped.
* @param {string} body - the body of the blockquote.
* @param {string} title - the title of the link.
* @param {string} url - the URL of the link.
* @returns {Promise<string>}
*/
export async function createBlockquote(text, title, url) {
text = escape(text)
.replaceAll('[', '\\[')
.replaceAll(']', '\\]')
.replaceAll('\n', '\n> ');

export async function createBlockquote(body, title, url) {
body = body.replaceAll('\n', '\n> ');
const link = await createLink(title, url);

return `> ${text}\n> \n> — ${link}`;
return `> ${body}\n> \n> — ${link}`;
}

/**
Expand Down

0 comments on commit ef68b2a

Please sign in to comment.