Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inkarnate integration MWP #314

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/@types/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface MapInterface {
export interface BlockParameters {
id?: string;
image?: string | string[];
inkarnate_id?: string;
layers?: string[];
tileServer?: string | string[];
tileOverlay?: string | string[];
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export default class ObsidianLeaflet
): Promise<void> {
/* try { */
/** Get Parameters from Source */
let params = getParamsFromSource(source);
let params = await getParamsFromSource(source);

if (!params.id) {
new Notice(t("Obsidian Leaflet maps must have an ID."));
Expand Down
25 changes: 23 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ type MarkerType =
* so it detects that to return them all correctly.
* 3. Next, it pulls out markers defined in the source block. This is clunky to support previous version's syntax, but works.
*/
export function getParamsFromSource(source: string): BlockParameters {
export async function getParamsFromSource(source: string): Promise<BlockParameters> {
let params: BlockParameters = {};

/** Pull out links */
Expand Down Expand Up @@ -277,7 +277,7 @@ export function getParamsFromSource(source: string): BlockParameters {
if ((source.match(/^\bimage\b:[\s\S]*?$/gm) ?? []).length > 1) {
layers = (source.match(/^\bimage\b:([\s\S]*?)$/gm) || []).map(
(p) => p.split("image: ")[1]
);
);
}

if (typeof params.image === "string") {
Expand All @@ -290,6 +290,27 @@ export function getParamsFromSource(source: string): BlockParameters {

params.layers = layers ?? [...image];

if (typeof params.inkarnate_id === "string") {
const response = await fetch(
`https://api2.inkarnate.com/api/publicScenes/${params.inkarnate_id}`,
{
method: "GET",
headers: {
Accept: "application/json",
},
}
);
if (response.ok)
{
let result = await response.json();
params.layers = [result["preview"]];
}
else
{
console.log(`Inkarnate map fetch failed! Status: ${response.status}`);
}
}

params.image = params.layers[0];

let obj: {
Expand Down