Skip to content

Commit

Permalink
Use indexOf instead of regex
Browse files Browse the repository at this point in the history
  • Loading branch information
NotWoods committed Sep 20, 2023
1 parent cc8c0b2 commit 1b5508f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
14 changes: 6 additions & 8 deletions src/svg.tsx → src/cover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function TeaDisplayGroup(props: {
teaList: readonly FormattedTeaDatabasePage[];
position: TranslatePosition;
}) {
// Hardcoded positions to layout each tea in a 2x3 grid
const positions = [
"30 85",
"30 180",
Expand All @@ -72,9 +73,6 @@ function TeaDisplayGroup(props: {
);
}

/** Matches </svg> */
const SVG_CLOSING_TAG = /<\/svg\s*>/;

/**
* Creates an SVG file to the current directory to use as an ebook cover.
* The image contains a summary of the given tea information.
Expand All @@ -86,7 +84,7 @@ const SVG_CLOSING_TAG = /<\/svg\s*>/;
* @param bottomDisplayTeas Items displayed on the bottom half of the cover.
* @returns Promise that resolves once the cover image is written.
*/
export async function generateSvg(
export async function generateSvgCover(
topDisplayTeas: readonly FormattedTeaDatabasePage[],
bottomDisplayTeas: readonly FormattedTeaDatabasePage[],
) {
Expand All @@ -95,12 +93,12 @@ export async function generateSvg(
new URL("../assets/cover.svg", import.meta.url),
);

const closingTagMatch = SVG_CLOSING_TAG.exec(svgTemplate);
if (!closingTagMatch) {
const closingTagIndex = svgTemplate.lastIndexOf('</svg');
if (!closingTagIndex) {
throw new Error("Could not find </svg> tag in template");
}
const templatePrefix = svgTemplate.slice(0, closingTagMatch.index);
const templateSuffix = svgTemplate.slice(closingTagMatch.index);
const templatePrefix = svgTemplate.slice(0, closingTagIndex);
const templateSuffix = svgTemplate.slice(closingTagIndex);

const generatedText: string = (
<g id="tea">
Expand Down
4 changes: 2 additions & 2 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
fetchTeaPageContent,
} from "./notion/client.tsx";
import type { FormattedTeaDatabasePage } from "./notion/types.ts";
import { generateSvg } from "./svg.tsx";
import { generateSvgCover } from "./cover.tsx";

interface TeaCollection {
topDisplayTeas: readonly FormattedTeaDatabasePage[];
Expand Down Expand Up @@ -115,7 +115,7 @@ const teaCollection = await getTeaCollection();
const { topDisplayTeas, bottomDisplayTeas } = teaCollection;

// Generate the cover image SVG
const svg = await generateSvg(topDisplayTeas, bottomDisplayTeas);
const svg = await generateSvgCover(topDisplayTeas, bottomDisplayTeas);
await Deno.writeTextFile("tea.svg", svg);

// Write the book file for pandoc
Expand Down

0 comments on commit 1b5508f

Please sign in to comment.