Skip to content

Commit f114799

Browse files
Extract removeImages function to strings.ts and update references in preview.ts for better code reuse.
1 parent cb53aaa commit f114799

2 files changed

Lines changed: 8 additions & 8 deletions

File tree

src/utils/preview.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
import { getImages } from "./image";
22
import type { Config } from "../types/config";
3-
import { titleCase } from "./strings";
3+
import { removeImages, titleCase } from "./strings";
44

55
const hasHeader = (content: string) => content.match(/^#\s+/);
66

7-
const cleanUp = (content: string): string =>
8-
content
9-
.replace(/^(#\s+.+[\n\s]+)\s*<picture>[.\w\W]+<\/picture>[\n\s]*/, "$1")
10-
.replace(/^(#\s+.+[\n\s]+)(!\[.+]\(.*\)\n?){1,2}[\n\s]*/, "$1")
11-
.replace(/^(#\s+.+[\n\s]+)(<img\s.*\/>\n?){1,2}[\n\s]*/, "$1");
12-
137
export const setPreview = (content: string, config: Config) => {
148
if (!hasHeader(content)) {
159
const title = titleCase(config.image.parameters.title);
@@ -21,7 +15,7 @@ export const setPreview = (content: string, config: Config) => {
2115

2216
const replace = "$1";
2317

24-
return cleanUp(content).replace(
18+
return removeImages(content).replace(
2519
/^(#\s+.+[\n\s]+)/,
2620
`${replace}${images}\n\n`,
2721
);

src/utils/strings.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export const titleCase = (title: string | undefined) => {
1010
.replace(/[-_]/g, " ");
1111
};
1212

13+
export const removeImages = (content: string): string =>
14+
content
15+
.replace(/^(#\s+.+[\n\s]+)\s*<picture>[.\w\W]+<\/picture>[\n\s]*/, "$1")
16+
.replace(/^(#\s+.+[\n\s]+)(!\[.+]\(.*\)\n?){1,2}[\n\s]*/, "$1")
17+
.replace(/^(#\s+.+[\n\s]+)(<img\s.*\/>\n?){1,2}[\n\s]*/, "$1");
18+
1319
export const encodeUri = (value: string | undefined): string => {
1420
if (value === "" || value === undefined) {
1521
return "";

0 commit comments

Comments
 (0)