Skip to content

Commit

Permalink
fix(page): add missing assets back (#6641)
Browse files Browse the repository at this point in the history
  • Loading branch information
fourdim committed Apr 1, 2024
1 parent 2f62c1a commit ac92598
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ describe('notion html to snapshot', () => {
test('embeded', async () => {
const html = `<div class="page-body">
<figure id="ed3d2ae9-62f5-433a-9049-9ddbd1c81ac5">
<div class="source"><a href="Untitled%ed3d2ae962f5433a90499ddbd1c81ac507/README.pdf">README.pdf</a>
<div class="source"><a href="Untitled%203d2ae962f5433a90499ddbd1c81ac507/README.pdf">README.pdf</a>
</div>
</figure>
</div>`;
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/_common/adapters/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ export class HtmlAdapter extends BaseAdapter<Html> {
if (!fetchable(imageURL)) {
assets.getAssets().forEach((_value, key) => {
const attachmentName = getAssetName(assets.getAssets(), key);
if (imageURL.includes(attachmentName)) {
if (decodeURIComponent(imageURL).includes(attachmentName)) {
blobId = key;
}
});
Expand Down
3 changes: 2 additions & 1 deletion packages/blocks/src/_common/adapters/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,8 @@ export class MarkdownAdapter extends BaseAdapter<Markdown> {
if (!fetchable(o.node.url)) {
const imageURL = o.node.url;
assets.getAssets().forEach((_value, key) => {
if (imageURL.includes(getAssetName(assets.getAssets(), key))) {
const imageName = getAssetName(assets.getAssets(), key);
if (decodeURIComponent(imageURL).includes(imageName)) {
blobId = key;
}
});
Expand Down
6 changes: 3 additions & 3 deletions packages/blocks/src/_common/adapters/notion-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export class NotionHtmlAdapter extends BaseAdapter<NotionHtml> {
if (!fetchable(imageURL)) {
assets.getAssets().forEach((_value, key) => {
const attachmentName = getAssetName(assets.getAssets(), key);
if (imageURL.includes(attachmentName)) {
if (decodeURIComponent(imageURL).includes(attachmentName)) {
blobId = key;
}
});
Expand Down Expand Up @@ -578,7 +578,7 @@ export class NotionHtmlAdapter extends BaseAdapter<NotionHtml> {
if (!fetchable(imageURL)) {
assets.getAssets().forEach((_value, key) => {
const attachmentName = getAssetName(assets.getAssets(), key);
if (imageURL.includes(attachmentName)) {
if (decodeURIComponent(imageURL).includes(attachmentName)) {
blobId = key;
}
});
Expand Down Expand Up @@ -636,7 +636,7 @@ export class NotionHtmlAdapter extends BaseAdapter<NotionHtml> {
if (!fetchable(embededURL)) {
assets.getAssets().forEach((value, key) => {
const embededName = getAssetName(assets.getAssets(), key);
if (embededURL.includes(embededName)) {
if (decodeURIComponent(embededURL).includes(embededName)) {
blobId = key;
name = embededName;
size = value.size;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '../../../../_common/components/loader.js';

import { WithDisposable } from '@blocksuite/block-std';
import { type DocCollection } from '@blocksuite/store';
import { type DocCollection, sha } from '@blocksuite/store';
import { Job } from '@blocksuite/store';
import JSZip from 'jszip';
import { html, LitElement, type PropertyValues } from 'lit';
Expand Down Expand Up @@ -70,6 +70,7 @@ export async function importNotion(collection: DocCollection, file: File) {
const pageMap = new Map<string, string>();
const files = Object.keys(zipFile.files);
const promises: Promise<void>[] = [];
const pendingAssets = new Map<string, Blob>();
for (let i = 0; i < files.length; i++) {
const file = files[i];
if (file.startsWith('__MACOSX/')) continue;
Expand All @@ -96,19 +97,27 @@ export async function importNotion(collection: DocCollection, file: File) {
}
}
pageMap.set(file, collection.idGenerator());
continue;
}
if (i === 0 && fileName.endsWith('.csv')) {
window.open(
'https://affine.pro/blog/import-your-data-from-notion-into-affine',
'_blank'
);
continue;
}
if (fileName.endsWith('.zip')) {
const innerZipFile = await zipFile.file(fileName)?.async('blob');
if (innerZipFile) {
promises.push(...(await parseZipFile(innerZipFile)));
}
continue;
}
const blob = await zipFile.files[file].async('blob');
pendingAssets.set(
await sha(await blob.arrayBuffer()),
new File([blob], fileName)
);
}
const pagePromises = Array.from(pageMap.keys()).map(async file => {
const job = new Job({
Expand All @@ -117,6 +126,12 @@ export async function importNotion(collection: DocCollection, file: File) {
});
const htmlAdapter = new NotionHtmlAdapter();
htmlAdapter.applyConfigs(job.adapterConfigs);
const assets = job.assetsManager.getAssets();
for (const [key, value] of pendingAssets.entries()) {
if (!assets.has(key)) {
assets.set(key, value);
}
}
const snapshot = await htmlAdapter.toDocSnapshot({
file: await zipFile.files[file].async('text'),
pageId: pageMap.get(file),
Expand Down

0 comments on commit ac92598

Please sign in to comment.