This repository has been archived by the owner on Mar 30, 2024. It is now read-only.
generated from tudoujunha/next-contentlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
278 additions
and
170 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { Paragraph, Link, Text } from 'mdast'; | ||
import type { Node, Literal, Parent } from 'unist'; | ||
|
||
function isObject(target: unknown): target is { [key: string]: unknown } { | ||
return typeof target === 'object' && target !== null; | ||
} | ||
|
||
export function isNode(node: unknown): node is Node { | ||
return isObject(node) && 'type' in node; | ||
} | ||
|
||
export function isParent(node: unknown): node is Parent { | ||
return isObject(node) && Array.isArray(node.children); | ||
} | ||
|
||
export function isLiteral(node: unknown): node is Literal { | ||
return isObject(node) && 'value' in node; | ||
} | ||
|
||
export function isParagraph(node: unknown): node is Paragraph { | ||
return isNode(node) && node.type === 'paragraph'; | ||
} | ||
|
||
export function isText(node: unknown): node is Text { | ||
return ( | ||
isLiteral(node) && node.type === 'text' && typeof node.value === 'string' | ||
); | ||
} | ||
|
||
export function isLink(node: unknown): node is Link { | ||
return isNode(node) && node.type === 'link'; | ||
} | ||
|
||
export function isBareLink( | ||
node: unknown | ||
): node is Paragraph & { children: [Link & { children: [Text] }] } { | ||
return ( | ||
isParagraph(node) && | ||
node.children.length === 1 && | ||
isLink(node.children[0]) && | ||
isText(node.children[0].children[0]) | ||
); | ||
} |
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,38 @@ | ||
import { visit } from 'unist-util-visit'; | ||
|
||
import type { Root } from 'mdast'; | ||
import type { Plugin } from 'unified'; | ||
import type { Parent } from 'unist'; | ||
import { isBareLink, isParent } from './mdast/node-is'; | ||
|
||
const LIST_ITEM = 'listItem'; | ||
|
||
export const RemarkLinkCard: Plugin<void[], Root> = () => { | ||
return (tree) => { | ||
visit(tree, isBareLink, (node, _index, parent: Parent | undefined) => { | ||
if (!isParent(parent)) { | ||
return; | ||
} | ||
|
||
if (parent.type === LIST_ITEM) { | ||
return; | ||
} | ||
|
||
const child = node.children[0]; | ||
|
||
if (!child.url.startsWith('http')) { | ||
return; | ||
} | ||
|
||
child.data = { | ||
...child.data, | ||
// TODO: fix | ||
// hProperties: { | ||
// ...(child.data?.hProperties ?? {}), | ||
// dataLinkcard: true, | ||
// }, | ||
}; | ||
console.log(child); | ||
}); | ||
}; | ||
}; |
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
Oops, something went wrong.