Skip to content

Commit

Permalink
Improve error handling in message processing and enhance Markdown con…
Browse files Browse the repository at this point in the history
…version logic
  • Loading branch information
tztsai committed Nov 9, 2024
1 parent ede47a2 commit 0966577
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 41 deletions.
19 changes: 15 additions & 4 deletions background/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ md.messages = ({storage: {defaults, state, set}, compilers, mathjax, xhr, webreq
markdown = jax.tokenize(markdown)
}

var html = compilers[state.compiler].compile(markdown)

try {
var html = compilers[state.compiler].compile(markdown)
} catch (err) {
sendResponse({error: err.message})
return;
}

if (state.content.mathjax) {
html = jax.detokenize(html)
}
Expand Down Expand Up @@ -44,8 +49,14 @@ md.messages = ({storage: {defaults, state, set}, compilers, mathjax, xhr, webreq
}, sendResponse)
}
else if (req.message === 'inject') {
console.error('injecting', req.id);
md.inject({storage: {state}})(req.tabId)
chrome.tabs.query({ }, (tabs) => {
for (var tab of tabs) {
if (tab.url === req.url) {
md.inject({storage: {state}})(tab.id);
break;
}
}
});
}

// popup
Expand Down
3 changes: 2 additions & 1 deletion content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ var update = (update) => {
setTimeout(() => mj.render(), 60)
}

!update && setTimeout(() => {
setTimeout(() => {
const headers = document.querySelectorAll('h2, h3, h4, h5, h6');
headers.forEach(header => {
header.classList.add('foldable-header');
Expand Down Expand Up @@ -141,6 +141,7 @@ var render = (md) => {
compiler: state.compiler,
markdown: frontmatter(state.markdown)
}, (res) => {
if (res.err) return;
state.html = res.html
if (state.content.emoji) {
state.html = emojinator(state.html)
Expand Down
58 changes: 22 additions & 36 deletions content/turndown.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
async function MDwise(content, use_ai = false) {
async function convertToMD(content) {
await import('/vendor/turndown.min.js');

const turndownService = new TurndownService();
turndownService.remove(['script', 'style', 'input', 'textarea', 'form', 'noscript', 'aside', 'nav', 'button']);
let markdown = turndownService.turndown(content);

if (!use_ai) return markdown;
turndownService.remove(['script', 'style', 'input', 'meta', 'textarea', 'form', 'noscript', 'aside', 'nav', 'button', 'link', 'img', 'svg', 'canvas', 'audio', 'video', 'iframe']);
return turndownService.turndown(content);
}

async function MDwise(text) {

const prompt = `Convert the following text provided by the user to a well-structured Markdown document. For large chunks of text, consider splitting them into smaller subsections. For each section of any level containing too much information for the user to easily digest, **write a brief summary under its header with prefix "> Summary: "**. Do your best to enable the user to clearly and quickly understand the whole document from top level to bottom.`;

const messageJson = {
model: "gpt-4o-mini",
messages: [
{ role: 'system', content: prompt },
{ role: 'user', content: markdown },
{ role: 'user', content: text },
],
stream: true,
max_tokens: 4096,
Expand All @@ -40,6 +40,8 @@ async function MDwise(content, use_ai = false) {
markdown = '';
buf = '';

const interval = setInterval(() => render(markdown), 1000);

while (true) {
const { done, value } = await reader.read();
if (done) break;
Expand All @@ -64,37 +66,21 @@ async function MDwise(content, use_ai = false) {
if (buf.length > 0) {
console.error('Error parsing JSON:', buf);
}
return markdown;
};

async function createNewTab(markdown) {
const blob = new Blob([markdown], { type: 'text/markdown;charset=utf-8' });
const url = URL.createObjectURL(blob);
return new Promise((resolve, reject) => {
chrome.tabs.create({ url: 'about:blank' }, (tab) => {
console.warn('Creating new tab with URL:', url);
if (chrome.runtime.lastError) {
return reject(chrome.runtime.lastError);
}
const tabId = tab.id;
chrome.scripting.executeScript({
target: { tabId },
func: (markdown) => {
document.body.innerHTML = `<pre>${markdown}</pre>`;
},
args: [markdown]
}, () => {
if (chrome.runtime.lastError) {
return reject(chrome.runtime.lastError);
}
resolve(tabId);
});
});
});
clearInterval(interval);
render(markdown);
};

function fixTurnDown(md) {
md = md.replace(/^(#*\s*)\[(\s*\n)+/gm, '$1[');
return md;
}

(async () => {
const md = await MDwise(document.body);
const tabId = await createNewTab(md);
chrome.runtime.sendMessage({ message: 'inject', tabId });
// convert the page to markdown
const md = fixTurnDown(await convertToMD(document.body));
document.body.innerHTML = `<pre>${md}</pre>`;
// inject the renderer
chrome.runtime.sendMessage({ message: 'inject', url: window.location.href });
await MDwise(md);
})()

0 comments on commit 0966577

Please sign in to comment.