jormungandr-patches/patches/newmfm/0001-add-border-mfm.patch
2024-07-14 20:32:39 -06:00

76 lines
2.7 KiB
Diff

From bb131259350d68a6081fa5202477ba090d163363 Mon Sep 17 00:00:00 2001
From: limepotato <limepot@protonmail.ch>
Date: Sun, 14 Jul 2024 05:46:32 -0600
Subject: [PATCH 1/5] add border mfm
---
packages/client/src/components/mfm.ts | 20 ++++++++++++++++++++
packages/client/src/scripts/safe-parse.ts | 11 +++++++++++
2 files changed, 31 insertions(+)
create mode 100644 packages/client/src/scripts/safe-parse.ts
diff --git a/packages/client/src/components/mfm.ts b/packages/client/src/components/mfm.ts
index a2c4fdcb7..32b72564b 100644
--- a/packages/client/src/components/mfm.ts
+++ b/packages/client/src/components/mfm.ts
@@ -14,6 +14,7 @@ import MkA from "@/components/global/MkA.vue";
import { host } from "@/config";
import { reducedMotion } from "@/scripts/reduced-motion";
import { defaultStore } from "@/store";
+import { safeParseFloat } from "@/scripts/safe-parse";
export default defineComponent({
props: {
@@ -70,6 +71,11 @@ export default defineComponent({
// : null
// }
+ const validColor = (c: unknown): string | null => {
+ if (typeof c !== 'string') return null;
+ return c.match(/^[0-9a-f]{3,6}$/i) ? c : null;
+ };
+
const genEl = (ast: mfm.MfmNode[]) =>
concat(
ast.map((token, index): VNode[] => {
@@ -300,6 +306,20 @@ export default defineComponent({
style = `background-color: #${color};`;
break;
}
+ case 'border': {
+ let color = validColor(token.props.args.color);
+ color = color ? `#${color}` : 'var(--accent)';
+ let b_style = token.props.args.style;
+ if (
+ typeof b_style !== 'string' ||
+ !['hidden', 'dotted', 'dashed', 'solid', 'double', 'groove', 'ridge', 'inset', 'outset']
+ .includes(b_style)
+ ) b_style = 'solid';
+ const width = safeParseFloat(token.props.args.width) ?? 1;
+ const radius = safeParseFloat(token.props.args.radius) ?? 0;
+ style = `border: ${width}px ${b_style} ${color}; border-radius: ${radius}px;${token.props.args.noclip ? '' : ' overflow: clip;'}`;
+ break;
+ }
case "small": {
return h(
"small",
diff --git a/packages/client/src/scripts/safe-parse.ts b/packages/client/src/scripts/safe-parse.ts
new file mode 100644
index 000000000..6bfcef6c3
--- /dev/null
+++ b/packages/client/src/scripts/safe-parse.ts
@@ -0,0 +1,11 @@
+/*
+ * SPDX-FileCopyrightText: syuilo and misskey-project
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+export function safeParseFloat(str: unknown): number | null {
+ if (typeof str !== 'string' || str === '') return null;
+ const num = parseFloat(str);
+ if (isNaN(num)) return null;
+ return num;
+}
--
2.45.2