Skip to content

Commit

Permalink
seventh commit
Browse files Browse the repository at this point in the history
  • Loading branch information
brklntmhwk committed Oct 30, 2024
1 parent 68eb948 commit 3d33ad4
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 89 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,50 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: latest

- name: Install Dependencies
run: bun install --no-save

- name: Format & Lint Code
run: bun --bun run check

build:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: latest

- name: Install Dependencies
run: bun install --no-save

- name: Build
run: bun --bun run build

test:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: latest

- name: Install Dependencies
run: bun install --no-save

- name: Test Code
run: bun test
178 changes: 89 additions & 89 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,98 +5,98 @@ import type { LeafDirective } from "mdast-util-directive";
import type { Plugin } from "unified";
import { visit } from "unist-util-visit";
import {
isContainerDirective,
isImage,
isLink,
isParagraph,
isText,
isContainerDirective,
isImage,
isLink,
isParagraph,
isText,
} from "./utils";

const remarkCard: Plugin<[], Root> = () => {
return (tree) => {
visit(tree, isContainerDirective, (node) => {
if (node.name !== "card-grid") return;
if (node.children.length === 0) return;

node.data = {
...node.data,
hProperties: {
class: node.attributes?.class,
},
};
});

visit(tree, isContainerDirective, (node) => {
if (node.name !== "card") return;
if (node.children.length === 0) return;

const [firstNode, secondNode, ..._restNodes] = node.children;
if (!isParagraph(firstNode)) return;
if (firstNode.children.length === 0) return;

let cardImageOrLink: PhrasingContent;
let cardContent: PhrasingContent[];
let cardLabel = "";

const imageWrapper: LeafDirective = {
type: "leafDirective",
name: "image-wrapper",
data: {
hName: "div",
},
children: [],
};

const content: LeafDirective = {
type: "leafDirective",
name: "card-content",
data: {
hName: "div",
},
children: [],
};

if (firstNode.data?.directiveLabel === true) {
if (!isText(firstNode.children[0])) return;

cardLabel = firstNode.children[0].value;

if (!isParagraph(secondNode)) return;
const [imageOrLink, ...restContent] = secondNode.children;
cardImageOrLink = imageOrLink;
cardContent = restContent;
} else {
const [imageOrLink, ...restContent] = firstNode.children;
cardImageOrLink = imageOrLink;
cardContent = restContent;
}

if (isImage(cardImageOrLink)) {
cardImageOrLink.alt = cardImageOrLink.alt || cardLabel;
} else if (isLink(cardImageOrLink)) {
const cardImage = cardImageOrLink.children[0];
if (!isImage(cardImage)) return;

cardImage.alt = cardImage.alt || cardLabel;
} else {
return;
}

imageWrapper.children.push(cardImageOrLink);

for (const contentElem of cardContent) {
content.children.push(contentElem);
}

node.data = {
...node.data,
hProperties: {
class: node.attributes?.class,
},
};
node.children.splice(0, Number.POSITIVE_INFINITY, imageWrapper, content);
});
};
return (tree) => {
visit(tree, isContainerDirective, (node) => {
if (node.name !== "card-grid") return;
if (node.children.length === 0) return;

node.data = {
...node.data,
hProperties: {
class: node.attributes?.class,
},
};
});

visit(tree, isContainerDirective, (node) => {
if (node.name !== "card") return;
if (node.children.length === 0) return;

const [firstNode, secondNode, ..._restNodes] = node.children;
if (!isParagraph(firstNode)) return;
if (firstNode.children.length === 0) return;

let cardImageOrLink: PhrasingContent;
let cardContent: PhrasingContent[];
let cardLabel = "";

const imageWrapper: LeafDirective = {
type: "leafDirective",
name: "image-wrapper",
data: {
hName: "div",
},
children: [],
};

const content: LeafDirective = {
type: "leafDirective",
name: "card-content",
data: {
hName: "div",
},
children: [],
};

if (firstNode.data?.directiveLabel === true) {
if (!isText(firstNode.children[0])) return;

cardLabel = firstNode.children[0].value;

if (!isParagraph(secondNode)) return;
const [imageOrLink, ...restContent] = secondNode.children;
cardImageOrLink = imageOrLink;
cardContent = restContent;
} else {
const [imageOrLink, ...restContent] = firstNode.children;
cardImageOrLink = imageOrLink;
cardContent = restContent;
}

if (isImage(cardImageOrLink)) {
cardImageOrLink.alt = cardImageOrLink.alt || cardLabel;
} else if (isLink(cardImageOrLink)) {
const cardImage = cardImageOrLink.children[0];
if (!isImage(cardImage)) return;

cardImage.alt = cardImage.alt || cardLabel;
} else {
return;
}

imageWrapper.children.push(cardImageOrLink);

for (const contentElem of cardContent) {
content.children.push(contentElem);
}

node.data = {
...node.data,
hProperties: {
class: node.attributes?.class,
},
};
node.children.splice(0, Number.POSITIVE_INFINITY, imageWrapper, content);
});
};
};

export default remarkCard;

0 comments on commit 3d33ad4

Please sign in to comment.