-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the Web Browsing feature for static websites
- Loading branch information
1 parent
dd8c50c
commit 2357149
Showing
5 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Readability } from '@mozilla/readability' | ||
|
||
import { processText } from './history.js' | ||
import { parseDOM, request, turndown } from '../commands.js' | ||
|
||
import type { Context } from '../types/context.js' | ||
|
||
const linkRegex = /(?<!<%.*)<(https:\/\/[^>]+?)>(?!.*%>)/gis | ||
|
||
export async function webBrowser(content: string, context: Context): Promise<string> { | ||
return await processText(content, context, async (content, context) => { | ||
for (const [fullMatch, pageURL] of content.matchAll(linkRegex)) { | ||
const disk = context.file.disk | ||
let webPage: string | undefined | ||
|
||
try { | ||
const pageDoc = parseDOM(await request(pageURL)) | ||
|
||
const baseTag = pageDoc.head.getElementsByTagName('base')[0] | ||
if (baseTag === undefined) pageDoc.head.appendChild(pageDoc.createElement('base')) | ||
|
||
pageDoc.head.getElementsByTagName('base')[0].href = pageURL | ||
const article = new Readability(pageDoc).parse() | ||
|
||
webPage = turndown(article?.content ?? 'No Content Found.') | ||
if (article?.title !== undefined) webPage = `# ${article.title}\n\n${webPage}` | ||
} catch (error: any) { | ||
const errorName: string = error?.message ?? 'Web Page Browsing Error' | ||
const errorJSON: string = '```json\n> ' + JSON.stringify(error) + '\n> ```' | ||
|
||
const errorFull = `\n\n> [!BUG]+ **${errorName}**\n> ${errorJSON}` | ||
await disk.appendFile(context.file.path, errorFull) | ||
} | ||
|
||
if (webPage === undefined) continue | ||
|
||
if (webPage === 'No Content Found.') { | ||
const webPageError = `\n\n> [!ERROR]- No Content from "${pageURL}"\n> ` | ||
await disk.appendFile(context.file.path, webPageError + webPage) | ||
|
||
continue | ||
} | ||
|
||
const webPageEsc = webPage.replace(/<(\/?[!a-z])/gi, '<\uFEFF$1') | ||
const webPageLabel = `\n\n> [!EXAMPLE]- Content from "${pageURL}"` | ||
const webPageQuote = '\n> ' + webPageEsc.split('\n').join('\n> ') | ||
|
||
await disk.appendFile(context.file.path, webPageLabel + webPageQuote) | ||
content = content.replace(fullMatch, `@${pageURL}\n"""\n${webPage}\n"""\n\n`) | ||
} | ||
|
||
return content | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters