Skip to content

Commit

Permalink
fix: external link adopt normal link
Browse files Browse the repository at this point in the history
  • Loading branch information
linyibing committed Mar 30, 2024
1 parent 2204639 commit b9b8a30
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/periodic/DailyRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ export class DailyRecord {
if (Array.isArray(data)) {
await Promise.all(
data.map(async (resource) => {
if (resource.externalLink) {
return;
}

const folder = `${this.settings.periodicNotesPath}/Attachments`;
const resourcePath = normalizePath(
`${folder}/${generateFileName(resource)}`
Expand All @@ -129,10 +133,6 @@ export class DailyRecord {
return;
}

if (resource.externalLink) {
return;
}

const resourceURL = `${origin}/o/r/${resource.name || resource.id}`;
const { data } = await this.axios.get(resourceURL, {
responseType: 'arraybuffer',
Expand Down
1 change: 1 addition & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export type ContextType = {
export type ResourceType = {
name?: string;
externalLink?: string;
type?: string;
id: string;
filename: string;
};
Expand Down
21 changes: 14 additions & 7 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ export function formatDailyRecord(record: DailyRecordType) {
const targetResourceLine = resourceList?.length // 资源文件
? '\n' +
resourceList
?.map(
(resource: ResourceType) => `\t - ![[${generateFileName(resource)}]]`
)
?.map((resource: ResourceType) => `\t- ${generateFileLink(resource)}`)
.join('\n')
: '';
const finalTargetContent =
Expand All @@ -133,11 +131,20 @@ export function formatDailyRecord(record: DailyRecordType) {
return [date, timeStamp, finalTargetContent].map(String);
}

export function generateFileLink(resource: ResourceType): string {
if (!resource.externalLink) {
return `![[${generateFileName(resource)}]]`;
}

const prefix = resource.type?.includes('image') ? '!' : ''; // only add ! for image type

return `${prefix}[${resource.name || resource.filename}](${
resource.externalLink
})`;
}

export function generateFileName(resource: ResourceType): string {
return (
resource.externalLink ||
`${resource.id}-${resource.filename.replace(/[/\\?%*:|"<>]/g, '-')}`
);
return `${resource.id}-${resource.filename.replace(/[/\\?%*:|"<>]/g, '-')}`;
}

export function logMessage(message: string, level: LogLevel = LogLevel.info) {
Expand Down

0 comments on commit b9b8a30

Please sign in to comment.