Skip to content

Commit

Permalink
fix: fix type-check (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDeBoey authored Jul 17, 2021
1 parent 9c8ae04 commit 61a54b5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
},
"dependencies": {
"@babel/runtime": "^7.14.6",
"@types/mdast": "^3.0.6",
"@types/hast": "^2.3.2",
"@types/mdast": "^3.0.7",
"hast-util-from-parse5": "^6.0.1",
"parse5": "^6.0.1",
"unified": "^9.2.2",
Expand Down
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type {Element, Root} from 'hast'
import fromParse5 from 'hast-util-from-parse5'
import parse5 from 'parse5'
import type {Plugin} from 'unified'
import type {Link, Parent, Text} from 'mdast'
import type {Link, Paragraph, Text} from 'mdast'
import visit from 'unist-util-visit'

type GottenHTML = string | null
Expand Down Expand Up @@ -46,8 +47,8 @@ type RemarkEmbedderOptions = {

// results in an AST node of type "root" with a single "children" node of type "element"
// so we return the first (and only) child "element" node
const htmlToHast = (string: string): Parent =>
(fromParse5(parse5.parseFragment(string)) as Parent).children[0] as Parent
const htmlToHast = (string: string): Element =>
(fromParse5(parse5.parseFragment(string)) as Root).children[0] as Element

const getUrlString = (url: string): string | null => {
const urlString = url.startsWith('http') ? url : `https://${url}`
Expand All @@ -74,9 +75,9 @@ const remarkEmbedder: Plugin<[RemarkEmbedderOptions]> = ({
)

return async tree => {
const nodeAndURL: Array<{parentNode: Parent; url: string}> = []
const nodeAndURL: Array<{parentNode: Paragraph; url: string}> = []

visit(tree, 'paragraph', (paragraphNode: Parent) => {
visit(tree, 'paragraph', (paragraphNode: Paragraph) => {
if (paragraphNode.children.length !== 1) {
return
}
Expand All @@ -89,14 +90,13 @@ const remarkEmbedder: Plugin<[RemarkEmbedderOptions]> = ({
node.type === 'link' &&
!node.title &&
node.children.length === 1 &&
node.children[0].value === node.url
if (!isText && !isValidLink) {
(node.children[0] as Text).value === node.url
if (!(isText || isValidLink)) {
return
}

const {url, value = url} = node as Link

const urlString = getUrlString(value as string)
const value = isText ? (node as Text).value : (node as Link).url
const urlString = getUrlString(value)
if (!urlString) {
return
}
Expand Down

0 comments on commit 61a54b5

Please sign in to comment.