버그 수정

This commit is contained in:
kim365my 2024-04-11 15:07:24 +09:00
parent aabb4302b0
commit aa52baa0e0
3 changed files with 48 additions and 33 deletions

View File

@ -1,11 +1,11 @@
{ {
"id": "sample-plugin", "id": "LinkThumbnail Plugin",
"name": "Sample Plugin", "name": "LinkThumbnail Plugin",
"version": "1.0.0", "version": "1.0.0",
"minAppVersion": "0.15.0", "minAppVersion": "0.15.0",
"description": "Demonstrates some of the capabilities of the Obsidian API.", "description": "This is a plugin that displays link thumbnails.",
"author": "Obsidian", "author": "Obsidian",
"authorUrl": "https://obsidian.md", "authorUrl": "https://obsidian.md",
"fundingUrl": "https://obsidian.md/pricing", "fundingUrl": "https://obsidian.md/pricing",
"isDesktopOnly": false "isDesktopOnly": true
} }

View File

@ -1,7 +1,6 @@
import {debounce, editorLivePreviewField} from "obsidian"; import {debounce, editorLivePreviewField} from "obsidian";
import {EditorView, Decoration, DecorationSet, ViewUpdate, ViewPlugin, WidgetType} from "@codemirror/view"; import {EditorView, Decoration, DecorationSet, ViewUpdate, ViewPlugin, WidgetType} from "@codemirror/view";
import {StateField, StateEffect, StateEffectType} from "@codemirror/state"; import {StateField, StateEffect, StateEffectType, Range} from "@codemirror/state";
import {Range} from "@codemirror/rangeset";
import {syntaxTree, tokenClassNodeProp} from "@codemirror/language"; import {syntaxTree, tokenClassNodeProp} from "@codemirror/language";
import LinkThumbnailPlugin from "./main"; import LinkThumbnailPlugin from "./main";
import { LinkThumbnailWidgetParams } from "./LinkThumbnailWidgetParams"; import { LinkThumbnailWidgetParams } from "./LinkThumbnailWidgetParams";

View File

@ -1,4 +1,4 @@
import { requestUrl } from "obsidian"; import { RequestUrlParam, requestUrl } from "obsidian";
import { decode } from 'iconv-lite'; import { decode } from 'iconv-lite';
interface ogData { interface ogData {
@ -14,7 +14,7 @@ const OGDATACHACHE = "ogDataCache"
const urlRegex = new RegExp("^(http:\\/\\/www\\.|https:\\/\\/www\\.|http:\\/\\/|https:\\/\\/)?[a-z0-9]+([\\-.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$"); const urlRegex = new RegExp("^(http:\\/\\/www\\.|https:\\/\\/www\\.|http:\\/\\/|https:\\/\\/)?[a-z0-9]+([\\-.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$");
// 로컬데이터 접근 // 로컬데이터 접근
function getLocalOgData(url: string) { function loadLocalOgData(url: string) {
const data = localStorage.getItem(OGDATACHACHE + url); const data = localStorage.getItem(OGDATACHACHE + url);
if (data) { if (data) {
const ogData: ogData = JSON.parse(data); const ogData: ogData = JSON.parse(data);
@ -25,30 +25,41 @@ function getLocalOgData(url: string) {
async function saveLocalOgData(url:string, ogData: ogData) { async function saveLocalOgData(url:string, ogData: ogData) {
const imgUrl = ogData.ogImage; const imgUrl = ogData.ogImage;
if (imgUrl !== "") { if (imgUrl !== "") {
const lastDot = imgUrl.lastIndexOf("."); try {
let imgType = imgUrl.substring(lastDot + 1, imgUrl.length).toLowerCase(); const lastDot = imgUrl.lastIndexOf(".");
let imgType = imgUrl.substring(lastDot + 1, imgUrl.length).toLowerCase();
const options: RequestUrlParam = {
url: imgUrl,
method: "POST",
contentType: `image/${imgType}`
}
const file = await requestUrl(options);
const fileArrayBuffer = file.arrayBuffer;
// 방법 1) blob 변환
// const fileBlob = new Blob([fileArrayBuffer], { type: `image/${imgType}`});
// // 파일 리더 생성
// const reader = new FileReader();
// reader.readAsDataURL(fileBlob);
// let base64String = "";
// reader.onloadend = () => {
// const base64 = reader.result;
// if (typeof base64 === "string") base64String = base64;
// };
// 방법 2) ArrayBuffer 자체를 base64로 변환
const uint8 = new Uint8Array(fileArrayBuffer);
const base64String = btoa(uint8.reduce((data, byte)=> {
return data + String.fromCharCode(byte);
}, ''));
if (imgType.includes("svg")) imgType += "+xml";
ogData.ogImage = `data:image/${imgType};charset=utf-8;base64,` + base64String;
const file = await requestUrl({url:imgUrl, contentType: `image/${imgType}`}); } catch (error) {
const fileArrayBuffer = file.arrayBuffer; console.log(error);
// 방법 1) blob 변환 }
// const fileBlob = new Blob([fileArrayBuffer], { type: `image/${imgType}`});
// // 파일 리더 생성
// const reader = new FileReader();
// reader.readAsDataURL(fileBlob);
// let base64String = "";
// reader.onloadend = () => {
// const base64 = reader.result;
// if (typeof base64 === "string") base64String = base64;
// };
// 방법 2) ArrayBuffer 자체를 base64로 변환
const uint8 = new Uint8Array(fileArrayBuffer);
const base64String = btoa(uint8.reduce((data, byte)=> {
return data + String.fromCharCode(byte);
}, ''));
if (imgType.includes("svg")) imgType += "+xml";
ogData.ogImage = `data:image/${imgType};charset=utf-8;base64,` + base64String;
} }
// 저장하기 전에 img 데이터를 url-> blob -> base64로 변환 후 저장 // 저장하기 전에 img 데이터를 url-> blob -> base64로 변환 후 저장
@ -60,7 +71,11 @@ async function getOgData(url: string) {
const urlArr = urlRegex.exec(url); const urlArr = urlRegex.exec(url);
if (urlArr) { if (urlArr) {
try { try {
const response = await requestUrl(url); const options: RequestUrlParam = {
url: url,
method: "POST",
}
const response = await requestUrl(options);
if (response && response.headers && response.headers['content-type'] && !response.headers['content-type']?.includes('text/')) { if (response && response.headers && response.headers['content-type'] && !response.headers['content-type']?.includes('text/')) {
throw new Error('Page must return a header content-type with text/'); throw new Error('Page must return a header content-type with text/');
} }
@ -101,6 +116,7 @@ async function getOgData(url: string) {
return data; return data;
} catch (error) { } catch (error) {
console.error(error); console.error(error);
return null;
} }
} }
} }
@ -119,7 +135,7 @@ function renderOgData(data:ogData) {
} }
export async function LinkThumbnailWidgetParams(url: string) { export async function LinkThumbnailWidgetParams(url: string) {
const data = getLocalOgData(url) || await getOgData(url); const data = loadLocalOgData(url) || await getOgData(url);
if (data) { if (data) {
const result = renderOgData(data); const result = renderOgData(data);
return result; return result;