Skip to content

Commit

Permalink
Update storage and inject functionality; move makeFoldable function t…
Browse files Browse the repository at this point in the history
…o mdwise.js
  • Loading branch information
tztsai committed Nov 11, 2024
1 parent 359562f commit f635a0c
Show file tree
Hide file tree
Showing 6 changed files with 407 additions and 310 deletions.
4 changes: 3 additions & 1 deletion background/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ md.inject = ({storage: {state}}) => (id) => {
icon: state.settings.icon,
}],
func: (_args) => {
args = _args
pre = document.querySelector('pre');
if (!pre) {
console.error('No <pre> element found');
return;
}
pre.style.visibility = 'hidden'
args = _args
},
injectImmediately: true
})
Expand All @@ -36,6 +36,8 @@ md.inject = ({storage: {state}}) => (id) => {
target: {tabId: id},
files: [
'/vendor/mithril.min.js',
'/vendor/readability.min.js',
'/vendor/turndown.min.js',
state.content.syntax && ['/vendor/prism.min.js', '/vendor/prism-autoloader.min.js', '/content/prism.js'],
state.content.emoji && '/content/emoji.js',
state.content.mermaid && ['/vendor/mermaid.min.js', '/vendor/panzoom.min.js', '/content/mermaid.js'],
Expand Down
37 changes: 10 additions & 27 deletions background/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,44 +47,27 @@ md.messages = ({ storage: { defaults, state, set }, compilers, mathjax, xhr, web
injectImmediately: true
}, sendResponse)
}
else if (req.message === 'turndown') {
else if (req.message === 'to-markdown') {
chrome.scripting.executeScript({
target: { tabId: sender.tab.id },
files: ['/vendor/turndown.min.js'],
injectImmediately: true
}, () => {
chrome.scripting.executeScript({
target: { tabId: sender.tab.id },
func: (content) => {
const turndownService = new TurndownService();
turndownService.remove(
['input', 'textarea', 'form', 'aside', 'nav', 'button', 'canvas', 'audio', 'video', 'label', 'datalist', 'keygen', 'output', 'progress', 'meter', 'menu', 'menuitem']
);
md = turndownService.turndown(content)
// .replace(/^(#*\s*)\[(\s*\n)+/gm, '$1[');
return `<pre>${md}</pre>`;
},
args: [req.content]
}, (html) => sendResponse({ tabId: sender.tab.id, html }));
}, sendResponse);
}
else if (req.message === 'readability') {
chrome.scripting.executeScript({
target: { tabId: sender.tab.id },
files: ['/vendor/readability.min.js'],
injectImmediately: true
files: ['/vendor/readability.min.js', '/vendor/turndown.min.js'],
}, () => {
chrome.scripting.executeScript({
target: { tabId: sender.tab.id },
func: () => {
try {
return new Readability(document).parse();
// let content = new Readability(document).parse().content;
let content = document.body;
content = new TurndownService().turndown(content);
content = `<pre>${content}</pre>`;
document.body.innerHTML = content;
} catch (err) {
return { err };
console.error('Readability error:', err);
}
}
}, sendResponse);
}, sendResponse);
sendResponse({ tabId: sender.tab.id });
});
}
else if (req.message === 'inject') {
md.inject({ storage: { state } })(req.tabId);
Expand Down
4 changes: 2 additions & 2 deletions background/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ md.storage.defaults = (compilers) => {
width: 'auto',
},
content: {
ai: false,
ai: true,
autoreload: false,
emoji: true,
mathjax: true,
mathjax: false,
mermaid: false,
syntax: true,
toc: true,
Expand Down
Loading

0 comments on commit f635a0c

Please sign in to comment.