Skip to content

Commit

Permalink
refactor(utils): ✨ use mdast util functions in Remark plugins
Browse files Browse the repository at this point in the history
Closes: #68
  • Loading branch information
brklntmhwk committed Sep 19, 2024
1 parent de9c520 commit 3d37af6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/content/blog/en/total-newbie-builds-mini-itx-linux-pc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type: blog
title: Total newbie builds Mini-ITX Linux PC (Error after Error)
publishedAt: 2024-09-19T01:48:09.453Z
fmContentType: blog
modifiedAt: 2024-09-19T05:12:40.656Z
modifiedAt: 2024-09-19T05:56:29.339Z
category:
metadata: en/categories
slug: learning
Expand Down Expand Up @@ -123,7 +123,7 @@ The power cable almost touches the fans of the GPU, the extremely thick cable of

Incidentally, I recommend you get velcro tapes for tieing cables. I bought them at DAISO. They are very versatile as you can see in the photo above!

> [!note] Lessons
> [!note]+ Lessons
> - Cable management is a very deep subject
> - Velcro tapes are very useful for tieing cables
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/mdast-is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
FootnoteReference,
InlineCode,
Link,
List,
Paragraph,
Parent,
Text,
Expand Down Expand Up @@ -65,3 +66,7 @@ export function isText(node: unknown): node is Text {
export function isTextOrInlineCode(node: unknown): node is Text | InlineCode {
return isText(node) || isInlineCode(node);
}

export function isList(node: unknown): node is List {
return is(node, 'list');
}
12 changes: 6 additions & 6 deletions src/plugins/remark-callout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { RemarkPlugin } from '@astrojs/markdown-remark';
import type { BlockContent, DefinitionContent, Paragraph, Root } from 'mdast';
import type { Plugin } from 'unified';
import { visit } from 'unist-util-visit';
import { isParent } from './mdast-is';
import { isParagraph, isParent, isText } from './mdast-is';

type Callout = {
type: string;
Expand Down Expand Up @@ -41,13 +41,13 @@ const remarkCallout: Plugin<[], Root> = (): ReturnType<RemarkPlugin> => {
if (node.children.length === 0) return;

const paragraphNode = node.children[0]!;
if (paragraphNode.type !== 'paragraph') return;
if (!isParagraph(paragraphNode)) return;

if (!isParent(paragraphNode)) return;
if (paragraphNode.children.length === 0) return;

const calloutNode = paragraphNode.children[0];
if (calloutNode?.type !== 'text') return;
if (!isText(calloutNode)) return;

const [calloutTypeTitle, ...calloutContent] =
calloutNode.value.split('\n');
Expand Down Expand Up @@ -78,7 +78,7 @@ const remarkCallout: Plugin<[], Root> = (): ReturnType<RemarkPlugin> => {
},
...node.children.splice(1),
];
if (contentNode[0]?.type !== 'paragraph') return;
if (!isParagraph(contentNode[0])) return;
if (calloutContent.length > 0) {
contentNode[0].children.push({
type: 'text',
Expand Down Expand Up @@ -107,7 +107,7 @@ const remarkCallout: Plugin<[], Root> = (): ReturnType<RemarkPlugin> => {

if (calloutContent.length <= 0) {
for (const [i, child] of paragraphNode.children.slice(1).entries()) {
if (child.type !== 'text') {
if (!isText(child)) {
titleNode.children.push(child);
continue;
}
Expand All @@ -120,7 +120,7 @@ const remarkCallout: Plugin<[], Root> = (): ReturnType<RemarkPlugin> => {
});
}
if (contentLines.length > 0) {
if (contentNode[0]?.type !== 'paragraph') return;
if (!isParagraph(contentNode[0])) return;
contentNode[0].children.push({
type: 'text',
value: contentLines.join('\n'),
Expand Down
16 changes: 8 additions & 8 deletions src/plugins/remark-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { RemarkPlugin } from '@astrojs/markdown-remark';
import type { BlockContent, DefinitionContent, Image, Link, Root } from 'mdast';
import type { Plugin } from 'unified';
import { visit } from 'unist-util-visit';
import { isParent, isText } from './mdast-is';
import { isLink, isList, isParagraph, isParent, isText } from './mdast-is';

const parseSign = (sign: string | undefined): string | undefined => {
if (sign === undefined || sign === '') return;
Expand All @@ -24,7 +24,7 @@ const parseSign = (sign: string | undefined): string | undefined => {

const remarkCard: Plugin<[], Root> = (): ReturnType<RemarkPlugin> => {
return (tree) => {
visit(tree, 'list', (node) => {
visit(tree, isList, (node) => {
if (!isParent(node)) return;
if (node.children.length === 0) return;

Expand All @@ -33,13 +33,13 @@ const remarkCard: Plugin<[], Root> = (): ReturnType<RemarkPlugin> => {
if (listItemNode.children.length === 0) continue;

const cardSignNode = listItemNode.children[0];
if (cardSignNode?.type !== 'paragraph') continue;
if (!isParagraph(cardSignNode)) continue;

if (!isParent(cardSignNode)) continue;
if (cardSignNode.children.length === 0) continue;

const cardSign = cardSignNode.children[0];
if (cardSign?.type !== 'text') continue;
if (!isText(cardSign)) continue;

const cardSignText = cardSign.value;
if (!cardSignText.startsWith('@')) continue;
Expand All @@ -49,7 +49,7 @@ const remarkCard: Plugin<[], Root> = (): ReturnType<RemarkPlugin> => {
const borderType = parseSign(sign);

const cardListNode = listItemNode.children[1];
if (cardListNode?.type !== 'list') continue;
if (!isList(cardListNode)) continue;

if (!isParent(cardListNode)) continue;
if (cardListNode.children.length === 0) continue;
Expand All @@ -60,7 +60,7 @@ const remarkCard: Plugin<[], Root> = (): ReturnType<RemarkPlugin> => {
if (cardListImageNode.children.length === 0) continue;

const cardImageNode = cardListImageNode.children[0];
if (cardImageNode?.type !== 'paragraph') continue;
if (!isParagraph(cardImageNode)) continue;

if (!isParent(cardImageNode)) continue;
if (cardImageNode.children.length === 0) continue;
Expand All @@ -76,7 +76,7 @@ const remarkCard: Plugin<[], Root> = (): ReturnType<RemarkPlugin> => {
alt: imageMap.get('alt'),
url: imageMap.get('url'),
} as Image;
} else if (cardImageOrLink?.type === 'link') {
} else if (isLink(cardImageOrLink)) {
const cardImage = cardImageOrLink.children[0];
if (cardImage?.type !== 'image') continue;

Expand Down Expand Up @@ -126,7 +126,7 @@ const remarkCard: Plugin<[], Root> = (): ReturnType<RemarkPlugin> => {
if (cardListContentNode.children.length === 0) return;

const cardContentNode = cardListContentNode.children[0];
if (cardContentNode?.type !== 'paragraph') return;
if (!isParagraph(cardContentNode)) return;

if (!isParent(cardContentNode)) return;
if (cardContentNode.children.length === 0) return;
Expand Down

0 comments on commit 3d37af6

Please sign in to comment.