mirror of
https://github.com/docmost/docmost.git
synced 2026-05-18 23:44:24 +08:00
fix imports
This commit is contained in:
@@ -14,6 +14,7 @@ import { getAttachmentFolderPath } from '../../../core/attachment/attachment.uti
|
|||||||
import { AttachmentType } from '../../../core/attachment/attachment.constants';
|
import { AttachmentType } from '../../../core/attachment/attachment.constants';
|
||||||
import { unwrapFromParagraph } from '../utils/import-formatter';
|
import { unwrapFromParagraph } from '../utils/import-formatter';
|
||||||
import { resolveRelativeAttachmentPath } from '../utils/import.utils';
|
import { resolveRelativeAttachmentPath } from '../utils/import.utils';
|
||||||
|
import { imageDimensionsFromData } from 'image-dimensions';
|
||||||
import { load } from 'cheerio';
|
import { load } from 'cheerio';
|
||||||
import pLimit from 'p-limit';
|
import pLimit from 'p-limit';
|
||||||
import { InjectQueue } from '@nestjs/bullmq';
|
import { InjectQueue } from '@nestjs/bullmq';
|
||||||
@@ -271,15 +272,37 @@ export class ImportAttachmentService {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { attachmentId, apiFilePath } = processFile(relPath);
|
const { attachmentId, apiFilePath, abs } = processFile(relPath);
|
||||||
|
|
||||||
const width = $img.attr('width') ?? '100%';
|
let width = $img.attr('width');
|
||||||
|
const height = $img.attr('height');
|
||||||
const align = $img.attr('data-align') ?? 'center';
|
const align = $img.attr('data-align') ?? 'center';
|
||||||
|
|
||||||
|
if (!width) {
|
||||||
|
try {
|
||||||
|
const buf = await fs.readFile(abs);
|
||||||
|
const natural = imageDimensionsFromData(new Uint8Array(buf));
|
||||||
|
if (natural) {
|
||||||
|
width = height
|
||||||
|
? String(
|
||||||
|
Math.round((natural.width / natural.height) * Number(height)),
|
||||||
|
)
|
||||||
|
: String(natural.width);
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
/* empty */
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!width) {
|
||||||
|
width = '600';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$img
|
$img
|
||||||
.attr('src', apiFilePath)
|
.attr('src', apiFilePath)
|
||||||
.attr('data-attachment-id', attachmentId)
|
.attr('data-attachment-id', attachmentId)
|
||||||
.attr('width', width)
|
.attr('width', width)
|
||||||
|
.attr('height', height)
|
||||||
.attr('data-align', align);
|
.attr('data-align', align);
|
||||||
|
|
||||||
unwrapFromParagraph($, $img);
|
unwrapFromParagraph($, $img);
|
||||||
@@ -420,7 +443,7 @@ export class ImportAttachmentService {
|
|||||||
const { attachmentId, apiFilePath, abs } = processFile(relPath);
|
const { attachmentId, apiFilePath, abs } = processFile(relPath);
|
||||||
const fileName = path.basename(abs);
|
const fileName = path.basename(abs);
|
||||||
|
|
||||||
const width = $oldDiv.attr('data-width') || '100%';
|
const width = $oldDiv.attr('data-width') || '600';
|
||||||
const align = $oldDiv.attr('data-align') || 'center';
|
const align = $oldDiv.attr('data-align') || 'center';
|
||||||
|
|
||||||
const $newDiv = $('<div>')
|
const $newDiv = $('<div>')
|
||||||
@@ -460,7 +483,7 @@ export class ImportAttachmentService {
|
|||||||
.attr('data-type', 'drawio')
|
.attr('data-type', 'drawio')
|
||||||
.attr('data-src', drawioSvg.apiFilePath)
|
.attr('data-src', drawioSvg.apiFilePath)
|
||||||
.attr('data-title', 'diagram')
|
.attr('data-title', 'diagram')
|
||||||
.attr('data-width', '100%')
|
.attr('data-width', '600')
|
||||||
.attr('data-align', 'center')
|
.attr('data-align', 'center')
|
||||||
.attr('data-attachment-id', drawioSvg.attachmentId);
|
.attr('data-attachment-id', drawioSvg.attachmentId);
|
||||||
|
|
||||||
@@ -531,7 +554,9 @@ export class ImportAttachmentService {
|
|||||||
|
|
||||||
// Post-process DOM elements to add file sizes after uploads complete
|
// Post-process DOM elements to add file sizes after uploads complete
|
||||||
// This avoids blocking file operations during initial DOM processing
|
// This avoids blocking file operations during initial DOM processing
|
||||||
const elementsNeedingSize = $('[data-attachment-id]:not([data-attachment-size])');
|
const elementsNeedingSize = $(
|
||||||
|
'[data-attachment-id]:not([data-attachment-size])',
|
||||||
|
);
|
||||||
for (const element of elementsNeedingSize.toArray()) {
|
for (const element of elementsNeedingSize.toArray()) {
|
||||||
const $el = $(element);
|
const $el = $(element);
|
||||||
const attachmentId = $el.attr('data-attachment-id');
|
const attachmentId = $el.attr('data-attachment-id');
|
||||||
|
|||||||
@@ -99,6 +99,15 @@ export function defaultHtmlFormatter($: CheerioAPI, $root: Cheerio<any>) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const COLUMN_LAYOUTS = [
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'two_equal',
|
||||||
|
'three_equal',
|
||||||
|
'four_equal',
|
||||||
|
'five_equal',
|
||||||
|
] as const;
|
||||||
|
|
||||||
export function notionFormatter($: CheerioAPI, $root: Cheerio<any>) {
|
export function notionFormatter($: CheerioAPI, $root: Cheerio<any>) {
|
||||||
// remove page header icon and cover image
|
// remove page header icon and cover image
|
||||||
$root.find('.page-header-icon').remove();
|
$root.find('.page-header-icon').remove();
|
||||||
@@ -109,6 +118,31 @@ export function notionFormatter($: CheerioAPI, $root: Cheerio<any>) {
|
|||||||
if (!$(el).text().trim()) $(el).remove();
|
if (!$(el).text().trim()) $(el).remove();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// columns
|
||||||
|
$root.find('div.column-list').each((_, el) => {
|
||||||
|
const $list = $(el);
|
||||||
|
const $cols = $list.find('div.column');
|
||||||
|
|
||||||
|
if ($cols.length <= 1) {
|
||||||
|
$list.replaceWith($cols.html() || '');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const layout = COLUMN_LAYOUTS[$cols.length] ?? 'two_equal';
|
||||||
|
let cells = '';
|
||||||
|
$cols.each((_, col) => {
|
||||||
|
const $col = $(col);
|
||||||
|
$col.children('div[style*="display:contents"]').each((_, wrapper) => {
|
||||||
|
$(wrapper).replaceWith($(wrapper).html() || '');
|
||||||
|
});
|
||||||
|
cells += `<div data-type="column">${$col.html()}</div>`;
|
||||||
|
});
|
||||||
|
|
||||||
|
$list.replaceWith(
|
||||||
|
`<div data-type="columns" data-layout="${layout}">${cells}</div>`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
// block math → mathBlock
|
// block math → mathBlock
|
||||||
$root.find('figure.equation').each((_: any, fig: any) => {
|
$root.find('figure.equation').each((_: any, fig: any) => {
|
||||||
const $fig = $(fig);
|
const $fig = $(fig);
|
||||||
|
|||||||
Reference in New Issue
Block a user