@adminforth/markdown 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.js +22 -5
  2. package/index.ts +22 -5
  3. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -111,25 +111,42 @@ export default class MarkdownPlugin extends AdminForthPlugin {
111
111
  };
112
112
  const editorRecordPkField = resourceConfig.columns.find(c => c.primaryKey);
113
113
  if (this.options.attachments) {
114
- const extractKeyFromUrl = (url) => url.replace(/^https:\/\/[^\/]+\/+/, '');
114
+ const stripQueryAndHash = (value) => value.split('#')[0].split('?')[0];
115
+ const extractKeyFromUrl = (url) => {
116
+ // Supports absolute https/http URLs and protocol-relative URLs.
117
+ // Returns the object key as a path without leading slashes.
118
+ try {
119
+ const normalized = url.startsWith('//') ? `https:${url}` : url;
120
+ const u = new URL(normalized);
121
+ return u.pathname.replace(/^\/+/, '');
122
+ }
123
+ catch (_a) {
124
+ // Fallback: strip scheme/host if it looks like a URL, otherwise treat as a path.
125
+ return stripQueryAndHash(url).replace(/^https?:\/\/[^\/]+\/+/, '').replace(/^\/+/, '');
126
+ }
127
+ };
115
128
  function getAttachmentMetas(markdown) {
116
129
  var _a, _b, _c;
117
130
  if (!markdown) {
118
131
  return [];
119
132
  }
120
133
  // Minimal image syntax: ![alt](src) or ![alt](src "title") or ![alt](src 'title')
121
- // We only track https URLs and only those that look like S3/AWS public URLs.
122
- const imageRegex = /!\[([^\]]*)\]\(\s*(https:\/\/[^\s)]+)\s*(?:\s+(?:"([^"]*)"|'([^']*)'))?\s*\)/g;
134
+ // We track external (http/https) and relative sources, but skip data: URLs.
135
+ const imageRegex = /!\[([^\]]*)\]\(\s*([^\s)]+)\s*(?:\s+(?:\"([^\"]*)\"|'([^']*)'))?\s*\)/g;
123
136
  const byKey = new Map();
124
137
  for (const match of markdown.matchAll(imageRegex)) {
125
138
  const altRaw = (_a = match[1]) !== null && _a !== void 0 ? _a : '';
126
139
  const srcRaw = match[2];
127
140
  const titleRaw = (_c = ((_b = match[3]) !== null && _b !== void 0 ? _b : match[4])) !== null && _c !== void 0 ? _c : null;
128
- const srcNoQuery = srcRaw.split('?')[0];
129
- if (!srcNoQuery.includes('s3') && !srcNoQuery.includes('amazonaws')) {
141
+ const srcTrimmed = srcRaw.trim().replace(/^<|>$/g, '');
142
+ if (!srcTrimmed || srcTrimmed.startsWith('data:')) {
130
143
  continue;
131
144
  }
145
+ const srcNoQuery = stripQueryAndHash(srcTrimmed);
132
146
  const key = extractKeyFromUrl(srcNoQuery);
147
+ if (!key) {
148
+ continue;
149
+ }
133
150
  byKey.set(key, {
134
151
  key,
135
152
  alt: altRaw,
package/index.ts CHANGED
@@ -122,7 +122,20 @@ export default class MarkdownPlugin extends AdminForthPlugin {
122
122
 
123
123
  type AttachmentMeta = { key: string; alt: string | null; title: string | null };
124
124
 
125
- const extractKeyFromUrl = (url: string) => url.replace(/^https:\/\/[^\/]+\/+/, '');
125
+ const stripQueryAndHash = (value: string) => value.split('#')[0].split('?')[0];
126
+
127
+ const extractKeyFromUrl = (url: string) => {
128
+ // Supports absolute https/http URLs and protocol-relative URLs.
129
+ // Returns the object key as a path without leading slashes.
130
+ try {
131
+ const normalized = url.startsWith('//') ? `https:${url}` : url;
132
+ const u = new URL(normalized);
133
+ return u.pathname.replace(/^\/+/, '');
134
+ } catch {
135
+ // Fallback: strip scheme/host if it looks like a URL, otherwise treat as a path.
136
+ return stripQueryAndHash(url).replace(/^https?:\/\/[^\/]+\/+/, '').replace(/^\/+/, '');
137
+ }
138
+ };
126
139
 
127
140
  function getAttachmentMetas(markdown: string): AttachmentMeta[] {
128
141
  if (!markdown) {
@@ -130,8 +143,8 @@ export default class MarkdownPlugin extends AdminForthPlugin {
130
143
  }
131
144
 
132
145
  // Minimal image syntax: ![alt](src) or ![alt](src "title") or ![alt](src 'title')
133
- // We only track https URLs and only those that look like S3/AWS public URLs.
134
- const imageRegex = /!\[([^\]]*)\]\(\s*(https:\/\/[^\s)]+)\s*(?:\s+(?:"([^"]*)"|'([^']*)'))?\s*\)/g;
146
+ // We track external (http/https) and relative sources, but skip data: URLs.
147
+ const imageRegex = /!\[([^\]]*)\]\(\s*([^\s)]+)\s*(?:\s+(?:\"([^\"]*)\"|'([^']*)'))?\s*\)/g;
135
148
 
136
149
  const byKey = new Map<string, AttachmentMeta>();
137
150
  for (const match of markdown.matchAll(imageRegex)) {
@@ -139,12 +152,16 @@ export default class MarkdownPlugin extends AdminForthPlugin {
139
152
  const srcRaw = match[2];
140
153
  const titleRaw = (match[3] ?? match[4]) ?? null;
141
154
 
142
- const srcNoQuery = srcRaw.split('?')[0];
143
- if (!srcNoQuery.includes('s3') && !srcNoQuery.includes('amazonaws')) {
155
+ const srcTrimmed = srcRaw.trim().replace(/^<|>$/g, '');
156
+ if (!srcTrimmed || srcTrimmed.startsWith('data:')) {
144
157
  continue;
145
158
  }
146
159
 
160
+ const srcNoQuery = stripQueryAndHash(srcTrimmed);
147
161
  const key = extractKeyFromUrl(srcNoQuery);
162
+ if (!key) {
163
+ continue;
164
+ }
148
165
  byKey.set(key, {
149
166
  key,
150
167
  alt: altRaw,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/markdown",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Markdown plugin for adminforth",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",