diff --git a/scripts/import/scan-image-refs.ts b/scripts/import/scan-image-refs.ts new file mode 100644 index 0000000..41af438 --- /dev/null +++ b/scripts/import/scan-image-refs.ts @@ -0,0 +1,9 @@ +const MD_IMG = /!\[[^\]]*\]\((\/images\/[^)\s]+)\)/g +const HTML_IMG = /]+src=["'](\/images\/[^"']+)["']/g + +export function scanImageRefs(source: string): string[] { + const set = new Set() + for (const m of source.matchAll(MD_IMG)) set.add(m[1]) + for (const m of source.matchAll(HTML_IMG)) set.add(m[1]) + return [...set] +} diff --git a/tests/int/scan-image-refs.int.spec.ts b/tests/int/scan-image-refs.int.spec.ts new file mode 100644 index 0000000..eb32450 --- /dev/null +++ b/tests/int/scan-image-refs.int.spec.ts @@ -0,0 +1,21 @@ +import { describe, expect, it } from 'vitest' +import { scanImageRefs } from '../../scripts/import/scan-image-refs' + +describe('scanImageRefs', () => { + it('finds markdown image syntax', () => { + const r = scanImageRefs('![alt](/images/foo.png)\n![x](/images/bar.jpg)') + expect(r.sort()).toEqual(['/images/bar.jpg', '/images/foo.png']) + }) + it('finds raw src', () => { + const r = scanImageRefs('') + expect(r).toEqual(['/images/foo.png']) + }) + it('dedupes', () => { + const r = scanImageRefs('![a](/images/x.png) ![b](/images/x.png)') + expect(r).toEqual(['/images/x.png']) + }) + it('ignores external URLs', () => { + const r = scanImageRefs('![a](https://example.com/x.png)') + expect(r).toEqual([]) + }) +})