fix(editor): support relative image reference path when importing zip with images (#12264)

Closes: [BS-3385](https://linear.app/affine-design/issue/BS-3385/markdown类型的导入,支持media文件和md文件不在同目录的情况)

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
  - Added utility functions to resolve and normalize image file paths in markdown and HTML imports.
  - Introduced middleware to provide full file path context during file import and transformation.
  - Added new types for improved asset and file management in zip imports.

- **Refactor**
  - Centralized and simplified image processing logic across HTML, Markdown, and Notion HTML adapters for improved maintainability.
  - Enhanced type safety and clarity in file and asset handling during zip imports.

- **Tests**
  - Added comprehensive tests for image file path resolution utility.

- **Documentation**
  - Improved inline code comments explaining file path resolution logic.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
donteatfriedrice
2025-05-14 02:30:30 +00:00
parent 2f8d8dbc1e
commit 26ece014f1
13 changed files with 320 additions and 215 deletions
@@ -4,9 +4,9 @@ import remarkParse from 'remark-parse';
import { unified } from 'unified';
import { describe, expect, it } from 'vitest';
import { remarkGfm } from '../../../adapters/markdown/gfm';
import { remarkCallout } from '../../../adapters/markdown/remark-plugins';
import type { MarkdownAST } from '../../../adapters/markdown/type';
import { remarkGfm } from '../../../../adapters/markdown/gfm';
import { remarkCallout } from '../../../../adapters/markdown/remark-plugins/remark-callout';
import type { MarkdownAST } from '../../../../adapters/markdown/type';
describe('remarkCallout plugin', () => {
function isBlockQuote(node: MarkdownAST): node is Blockquote {
@@ -0,0 +1,57 @@
import { describe, expect, it } from 'vitest';
import { getImageFullPath } from '../../../adapters/utils/file-path';
describe('getImageFullPath', () => {
it('should resolve relative image paths correctly', () => {
const filePath = 'path/to/markdown/file.md';
// Test relative path in same directory
expect(getImageFullPath(filePath, 'image.png')).toBe(
'path/to/markdown/image.png'
);
// Test relative path in subdirectory
expect(getImageFullPath(filePath, 'images/photo.jpg')).toBe(
'path/to/markdown/images/photo.jpg'
);
// Test relative path in subdirectory
expect(getImageFullPath(filePath, './images/photo.jpg')).toBe(
'path/to/markdown/images/photo.jpg'
);
// Test relative path with parent directory
expect(getImageFullPath(filePath, '../images/photo.jpg')).toBe(
'path/to/images/photo.jpg'
);
// Test relative path with multiple parent directories
expect(getImageFullPath(filePath, '../../images/photo.jpg')).toBe(
'path/images/photo.jpg'
);
// Test relative path with multiple parent directories (which is not supported)
expect(getImageFullPath(filePath, '../../../../images/photo.jpg')).toBe(
'images/photo.jpg'
);
});
it('should handle absolute image paths correctly', () => {
const filePath = 'path/to/markdown/file.md';
// Test absolute path
expect(getImageFullPath(filePath, '/images/photo.jpg')).toBe(
'images/photo.jpg'
);
});
it('should handle URL-encoded image paths correctly', () => {
const filePath = 'path/to/markdown/file.md';
// Test URL-encoded spaces
expect(getImageFullPath(filePath, 'my%20photo.jpg')).toBe(
'path/to/markdown/my photo.jpg'
);
});
});