-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
74 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { Content, extractContentUsingMozillaReadability } from './contentExtractor' | ||
|
||
describe('extractContentUsingMozillaReadability', () => { | ||
test('extracts content', () => | ||
expect( | ||
extractContentUsingMozillaReadability({ | ||
id: 1, | ||
text: '<html><head><title>Bar - MySite</title></head><body><aside><nav><h1><a href="/">MySite</a></h1> <a href="/foo">foo</a></nav></aside><main><h1>Bar</h1> <p>Baz</p></main></body>', | ||
}) | ||
).toEqual<Content>({ | ||
title: 'Bar - MySite', | ||
content: '<main><h2>Bar</h2> <p>Baz</p></main>', | ||
textContent: 'Bar Baz', | ||
})) | ||
}) |
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,34 @@ | ||
import { Readability } from '@mozilla/readability' | ||
import { JSDOM } from 'jsdom' | ||
import { type Doc } from './doc' | ||
|
||
export interface Content { | ||
/** | ||
* Title of the document. | ||
*/ | ||
title: string | ||
|
||
/** | ||
* Content of the document, including some markup. Omits non-content-related elements (header, | ||
* footer, navigation, etc.). | ||
*/ | ||
content: string | ||
|
||
/** | ||
* Text content of the document, with all markup removed. Omits all non-content-related elements. | ||
*/ | ||
textContent: string | ||
} | ||
|
||
export type ContentExtractor = (doc: Doc) => Content | null | ||
|
||
export const extractContentUsingMozillaReadability: ContentExtractor = doc => { | ||
const info = new Readability(new JSDOM(doc.text, { url: doc.url }).window.document).parse() | ||
return info | ||
? { | ||
title: info.title, | ||
content: info.content.replace(/^<div id="readability-page-1" class="page">(.*)<\/div>$/, '$1'), | ||
textContent: info.textContent, | ||
} | ||
: null | ||
} |
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 was deleted.
Oops, something went wrong.
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