Skip to content

Commit

Permalink
feat(lyricova): shift hanging punctuations in OpenGraph posters
Browse files Browse the repository at this point in the history
  • Loading branch information
blueset authored Aug 22, 2024
1 parent e28fbb4 commit 3562c7f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Fragment, useEffect, useRef } from "react";
import classes from "./PlainTextHangingPunct.module.scss";
import { shiftinPuncts } from "src/utils/typography";

function CollapsedSpan({ children }: { children: string }) {
const ref = useRef<HTMLSpanElement>(null);
Expand Down Expand Up @@ -29,17 +30,6 @@ interface PlainTextHangingPunctProps {
children: string;
}

function shiftinPuncts(line: string, match: RegExpMatchArray | null, start: string, end: string ): RegExpMatchArray | null {
if (match && line.match(new RegExp(`${start}.*${end}`, "g"))) {
const front = match[1].split(start);
const back = match[3].split(end);
match[2] = `${start.repeat(front.length - 1)}${match[2]}${end.repeat(back.length - 1)}`;
match[1] = front.join("");
match[3] = back.join("");
}
return match;
}

export function PlainTextHangingPunct({
children,
}: PlainTextHangingPunctProps) {
Expand Down
8 changes: 7 additions & 1 deletion packages/lyricova/src/controllers/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Resvg } from "@resvg/resvg-js";
import { Readable } from "stream";
import React from "react";
import satori from "satori";
import { shiftinPuncts } from "src/utils/typography";

const sourceHanExtraLight = readFile(
resolve(__dirname, "../../src/fonts/SourceHanSans-ExtraLight-Subset-hhea.otf")
Expand Down Expand Up @@ -192,7 +193,12 @@ export class PublicApiController {
const lines = mainVerse.text
.replace(/——/g, "⸺")
.split("\n")
.map((line) => line.match(/^([\p{Ps}\p{Pi}"]*)(.*)$/u));
.map((line) => {
let match = line.match(/^([\p{Ps}\p{Pi}"]*)(.*)$/u);
match = shiftinPuncts(line, match, "「", "」");
match = shiftinPuncts(line, match, "『", "』");
match = shiftinPuncts(line, match, "「", "」");
});

const [
sourceHanExtraLightData,
Expand Down
11 changes: 11 additions & 0 deletions packages/lyricova/src/utils/typography.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function shiftinPuncts(line: string, match: RegExpMatchArray | null, start: string, end: string ): RegExpMatchArray | null {
if (match && line.match(new RegExp(`${start}.*${end}`, "g"))) {
const front = match[1].split(start);
const back = match[3].split(end);
match[2] = `${start.repeat(front.length - 1)}${match[2]}${end.repeat(back.length - 1)}`;
match[1] = front.join("");
match[3] = back.join("");
}
return match;
}

0 comments on commit 3562c7f

Please sign in to comment.