-
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.
- Loading branch information
Showing
12 changed files
with
1,059 additions
and
146 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,30 @@ | ||
import type { Cheerio, Element } from 'cheerio' | ||
import { getInnerText } from './textUtils.js' | ||
|
||
const MATCH_ID_AND_CLASS = /byline|author|dateline|writtenby|p-author/i | ||
|
||
export function extractByline($node: Cheerio<Element>, matchString: string) { | ||
const text = getInnerText($node) | ||
if (!isValidByline($node.text())) return | ||
|
||
const isAuthor = $node.attr('rel') === 'author' | ||
if (isAuthor) return text | ||
|
||
const hasAuthorItemprop = $node.attr('itemprop')?.includes('author') | ||
if (hasAuthorItemprop) return text | ||
|
||
const bylineMatch = MATCH_ID_AND_CLASS.test(matchString) | ||
if (bylineMatch) return text | ||
|
||
return | ||
} | ||
|
||
/** | ||
* Check whether the input string could be a byline. | ||
* This verifies that the input is a string, and that the length | ||
* is less than 100 chars. | ||
*/ | ||
export function isValidByline(byline: string) { | ||
const length = byline.trim().length | ||
return length > 0 && length < 100 | ||
} |
Oops, something went wrong.