Skip to content

Commit e4d98fe

Browse files
better serialize assets -> markdown
1 parent b3e29ad commit e4d98fe

File tree

3 files changed

+62
-22
lines changed

3 files changed

+62
-22
lines changed

admin/src/components/ReactMdEditor.tsx

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import "@uiw/react-markdown-preview/markdown.css";
1111
import {PLUGIN_ID} from '../utils/pluginId';
1212
import MediaLib from "./MediaLib";
1313
import { useField } from "@strapi/strapi/admin";
14+
import assetsToMarkdown from '../utils/assetsToMarkdown';
1415

1516
const Wrapper = styled.div`
1617
flex-basis: 100%;
@@ -100,28 +101,10 @@ const Editor: FunctionComponent<EditorProps> = ({
100101
const handleToggleMediaLib = () => setMediaLibVisible((prev) => !prev);
101102

102103
const handleChangeAssets = (assets: Schema.Attribute.MediaValue<true>) => {
103-
let newValue = value ? value : "";
104-
105-
assets.map((asset) => {
106-
if (asset.mime.includes("image")) {
107-
const imgTag = `![${asset.alt}](${asset.url})`;
108-
if (mediaLibSelection > -1) {
109-
const preValue = value?.substring(0, mediaLibSelection) ?? "";
110-
const postValue = value?.substring(mediaLibSelection) ?? "";
111-
newValue = `${
112-
preValue && !preValue.endsWith(" ") ? preValue + " " : preValue
113-
}${imgTag}${
114-
postValue && !postValue.startsWith(" ")
115-
? " " + postValue
116-
: postValue
117-
}`;
118-
} else {
119-
newValue = `${newValue}${imgTag}`;
120-
}
121-
}
122-
// Handle videos and other type of files by adding some code
123-
});
124-
onChange({ target: { name, value: newValue ?? "" } });
104+
105+
const output = value + assetsToMarkdown(assets);
106+
107+
onChange({ target: { name, value: output} });
125108
handleToggleMediaLib();
126109
};
127110

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import type { Schema } from "@strapi/types";
2+
import typeFromMime from '../utils/typeFromMime';
3+
4+
function arrayToMarkdownList(lines: string[]): string {
5+
if (lines.length === 1) {
6+
return lines[0];
7+
}
8+
return '\n' + lines.map(line => `* ${line}`).join('\n');
9+
}
10+
11+
12+
function singleAssetToMarkdown(asset:Schema.Attribute.MediaValue): string {
13+
const type = typeFromMime(asset.mime);
14+
let output = '';
15+
16+
switch(type){
17+
//media
18+
case 'image':
19+
case 'video':
20+
case 'audio':
21+
output = `![${asset.alt}](${asset.url})`;
22+
break;
23+
break;
24+
//doc
25+
default:
26+
output = `[${asset.alt}](${asset.url})`;
27+
break;
28+
}
29+
return output;
30+
}
31+
32+
export default function assetsToMarkdown(assets: Schema.Attribute.MediaValue<true>): string {
33+
34+
const lines = assets.map((asset) => {
35+
return singleAssetToMarkdown(asset);
36+
});
37+
38+
return arrayToMarkdownList(lines);
39+
40+
};

admin/src/utils/typeFromMime.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//strapi/packages/core/upload/admin/src/utils/typeFromMime.ts
2+
3+
const typeFromMime = (mime: string) => {
4+
if (mime.includes('image')) {
5+
return 'image';
6+
}
7+
if (mime.includes('video')) {
8+
return 'video';
9+
}
10+
if (mime.includes('audio')) {
11+
return'audio';
12+
}
13+
14+
return 'doc'
15+
};
16+
17+
export default typeFromMime;

0 commit comments

Comments
 (0)