Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: minor code improvements #4

Merged
merged 5 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions src/is-github-alert-declaration.ts

This file was deleted.

29 changes: 29 additions & 0 deletions src/is-github-alert-type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, expect, it } from "vitest";
import { GithubAlertType } from "./github-alert.type.js";
import { isGithubAlertType } from "./is-github-alert-type.js";

describe("is-github-alert-type", () => {
it("should return false for an undefined value", () => {
expect(isGithubAlertType(undefined)).toEqual(false);
});

it("should return false for a null value", () => {
expect(isGithubAlertType(null)).toEqual(false);
});

it("should return false for a string value not equal to a GitHub alert type", () => {
expect(isGithubAlertType("test")).toEqual(false);
});

it("should return true for a value directly taken from the GitHub alert type enum", () => {
expect(isGithubAlertType(GithubAlertType.WARNING)).toEqual(true);
});

it("should return true for a string value that equals an alert type", () => {
expect(isGithubAlertType("IMPORTANT")).toEqual(true);
});

it("should return false for a string value with the wrong capitalization", () => {
expect(isGithubAlertType("important")).toEqual(false);
});
});
8 changes: 8 additions & 0 deletions src/is-github-alert-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {
GITHUB_ALERT_TYPES,
type GithubAlertType,
} from "./github-alert.type.js";

export function isGithubAlertType(type: unknown): type is GithubAlertType {
return GITHUB_ALERT_TYPES.includes(type as GithubAlertType);
}
16 changes: 8 additions & 8 deletions src/parse-github-alert-blockquote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import { GithubAlertType } from "./github-alert.type.js";
import { parseGithubAlertBlockquote } from "./parse-github-alert-blockquote.js";

describe("parse-github-alert-blockquote", () => {
it("should return false if the blockquote has no children", () => {
it("should return null if the blockquote has no children", () => {
const blockquote: Blockquote = {
type: "blockquote",
children: [],
};

const result = parseGithubAlertBlockquote(blockquote);

expect(result).toBe(false);
expect(result).toBe(null);
});

it("should return false if the first chlid of the blockquote is not a paragraph", () => {
it("should return null if the first child of the blockquote is not a paragraph", () => {
const blockquote: Blockquote = {
type: "blockquote",
children: [
Expand All @@ -34,10 +34,10 @@ describe("parse-github-alert-blockquote", () => {

const result = parseGithubAlertBlockquote(blockquote);

expect(result).toBe(false);
expect(result).toBe(null);
});

it("should return false if the first child of the paragraph is not a text node", () => {
it("should return null if the first child of the paragraph is not a text node", () => {
const blockquote: Blockquote = {
type: "blockquote",
children: [
Expand All @@ -60,10 +60,10 @@ describe("parse-github-alert-blockquote", () => {

const result = parseGithubAlertBlockquote(blockquote);

expect(result).toBe(false);
expect(result).toBe(null);
});

it("should return false if the first paragraph child doesn't contain a valid alert declaration", () => {
it("should return null if the first paragraph child doesn't contain a valid alert declaration", () => {
const blockquote: Blockquote = {
type: "blockquote",
children: [
Expand All @@ -81,7 +81,7 @@ describe("parse-github-alert-blockquote", () => {

const result = parseGithubAlertBlockquote(blockquote);

expect(result).toBe(false);
expect(result).toBe(null);
});

it("should return the parsed alert if the blockquote is a valid alert", () => {
Expand Down
14 changes: 6 additions & 8 deletions src/parse-github-alert-blockquote.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
import type { Blockquote, Paragraph, Text } from "mdast";
import type { GithubAlert } from "./github-alert.type.js";
import { parseGithubAlertDeclaration } from "./is-github-alert-declaration.js";
import { parseGithubAlertDeclaration } from "./parse-github-alert-declaration.js";

/**
* Function that checks if a given blockquote is a GitHub alert and returns the
* parsed alert if it is.
*/
export function parseGithubAlertBlockquote(
node: Blockquote,
): false | GithubAlert {
): GithubAlert | null {
const [firstChild, ...blockQuoteChildren] = node.children;

if (firstChild === undefined) return false;
if (firstChild.type !== "paragraph") return false;
if (firstChild?.type !== "paragraph") return null;

const [firstParagraphChild, ...paragraphChildren] = firstChild.children;

if (firstParagraphChild === undefined) return false;
if (firstParagraphChild.type !== "text") return false;
if (firstParagraphChild?.type !== "text") return null;

const [possibleTypeDeclaration, ...textNodes] =
firstParagraphChild.value.split("\n");

if (possibleTypeDeclaration === undefined) return false;
if (possibleTypeDeclaration === undefined) return null;

const type = parseGithubAlertDeclaration(possibleTypeDeclaration);

if (type === false) return false;
if (type === null) return null;

const textNodeChildren: Text[] =
textNodes.length > 0 ? [{ type: "text", value: textNodes.join("\n") }] : [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { describe, expect, it } from "vitest";
import { GithubAlertType } from "./github-alert.type.js";
import { parseGithubAlertDeclaration } from "./is-github-alert-declaration.js";
import { parseGithubAlertDeclaration } from "./parse-github-alert-declaration.js";

describe("is-github-alert-declaration", () => {
it("should return false when passing an empty string.", () => {
describe("parse-github-alert-declaration", () => {
it("should return null when passing an empty string.", () => {
const result = parseGithubAlertDeclaration("");
expect(result).toBe(false);
expect(result).toBe(null);
});

it("should return false when passing a string that's not a Github alert declaration.", () => {
it("should return null when passing a string that is not a Github alert declaration.", () => {
const resultOne = parseGithubAlertDeclaration("test");
expect(resultOne).toBe(false);
expect(resultOne).toBe(null);

const resultTwo = parseGithubAlertDeclaration("[!TEST]");
expect(resultTwo).toBe(false);
expect(resultTwo).toBe(null);
});

it("should return the alert type when passing a string that is a Github alert declaration.", () => {
Expand Down
22 changes: 22 additions & 0 deletions src/parse-github-alert-declaration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { type GithubAlertType } from "./github-alert.type.js";
import { isGithubAlertType } from "./is-github-alert-type.js";

const GITHUB_ALERT_DECLARATION_REGEX = /^\s*\[\!(?<type>\w+)\]\s*$/;

/**
* Function that checks if a given string is a GitHub alert declaration and
* returns the parsed alert type if it is.
*
* A GitHub alert declaration is a string that is structured like this:
*
* `[!TYPE]`
*/
export function parseGithubAlertDeclaration(
text: string,
): GithubAlertType | null {
const match = text.match(GITHUB_ALERT_DECLARATION_REGEX);

const type = match?.groups?.type;

return isGithubAlertType(type) ? type : null;
}
2 changes: 1 addition & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const remarkGithubAdmonitionsToDirectives: Plugin<[], Root> = () => {
(node: Blockquote, index?: number, parent?: Parent) => {
const githubAlert = parseGithubAlertBlockquote(node);

if (githubAlert === false) return;
if (githubAlert === null) return;

const directive: ContainerDirective = {
type: "containerDirective",
Expand Down