Skip to content

Commit

Permalink
feat: added support for custom style in code block
Browse files Browse the repository at this point in the history
docs: added documentation

refs: #17
  • Loading branch information
aaronczichon committed May 14, 2024
1 parent 2329cd2 commit 4d99644
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
31 changes: 30 additions & 1 deletion DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Longitude: -63.57530151565183
It doesn't matter if you provide the latitude or longitude first. The plugin will recognize the values and generate the map image.
Also it's not relevant if you write `Latitude` or `latitude` and `Longitude` or `longitude`.

## Changing the icon
### Changing the icon

There are two options to change the icon. You can either use the marker icon setting in the plugin settings. This will override th icon on all maps globally.
If you want a custom icon for a specific map, you can add a `marker-url` field to the code block. The value of the field should be the URL to the icon you want to use.
Expand All @@ -46,6 +46,35 @@ maki: fire-station
This is the block above rendered with a custom maki icon:
![Screenshot mapbox maki icon](./docs/custom_maki.png)

### Change style

You can change the style of the map by selecting a different style in the plugin settings.
![Screenshot map styles](./docs/map-style-settings.png)

This setting changes every map in your vault and is plugin-global.
To change the style of a map only for a specific map, you can add a `style` field to the code block. The value of the field should be the name of the style you want to use.

```location
latitude: 44.64266326577057
longitude: -63.57530151565183
maki: fire-station
style: navigation-night-v1
```

The result looks like this:
![Screenshot: map with custom style](./docs/code-style-result.png)

Following values for the code block are supported:

- streets-v12
- outdoors-v12
- light-v11
- dark-v11
- satellite-v9
- satellite-streets-v12
- navigation-day-v1
- navigation-night-v1

**Hint**
Maki icon can only defined in the code block and can't be defined globally. If no custom marker or maki icon is defined, the map falls back to the default marker icon (a home icon).
If you have defined a custom icon URL (in plugin settings or in your code block) the defined maki icon is ignored.
Binary file added docs/code-style-result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/map-style-settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions src/functions/process-code.func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ export const processCodeBlock = (source: string) => {
const longitude = findLongitude(rows);
const markerUrl = findMarkerUrl(rows);
const makiIcon = findMarkerIcon(rows);
const mapStyle = findMapStyle(rows);

return {
latitude,
longitude,
markerUrl,
makiIcon,
mapStyle,
};
};

Expand Down Expand Up @@ -47,3 +49,10 @@ const findMarkerIcon = (rows: string[]) => {
makiIcon = makiIcon.toLocaleLowerCase().replace("maki:", "").trim();
return makiIcon;
};

const findMapStyle = (rows: string[]) => {
let mapStyle = rows.find((l) => l.toLowerCase().startsWith("style:"));
if (mapStyle)
mapStyle = mapStyle.toLocaleLowerCase().replace("style:", "").trim();
return mapStyle;
};
6 changes: 5 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default class MapboxPlugin extends Plugin {
extractedData.longitude,
extractedData.markerUrl,
extractedData.makiIcon,
extractedData.mapStyle,
);

const imageElement = document.createElement("img");
Expand All @@ -56,6 +57,7 @@ export default class MapboxPlugin extends Plugin {
longitude: string,
codeMarker: string = "",
makiIcon: string = "",
style: string = "",
): string => {
const mapboxAccessToken = this.settings.mapboxToken;
if (!mapboxAccessToken) {
Expand All @@ -66,7 +68,9 @@ export default class MapboxPlugin extends Plugin {
}
const markerUrl = this.getMarkerUrl(codeMarker, makiIcon);

const imageUrl = `https://api.mapbox.com/styles/v1/mapbox/${this.settings.mapStyle}/static/${markerUrl}(${longitude},${latitude})/${longitude},${latitude},14/800x400?access_token=${mapboxAccessToken}`;
const mapStyle = style || this.settings.mapStyle;

const imageUrl = `https://api.mapbox.com/styles/v1/mapbox/${mapStyle}/static/${markerUrl}(${longitude},${latitude})/${longitude},${latitude},14/800x400?access_token=${mapboxAccessToken}`;

return imageUrl;
};
Expand Down

0 comments on commit 4d99644

Please sign in to comment.