Skip to content

Commit 371e985

Browse files
authored
Merge pull request #1289 from hackmdio/feature/embed-geolocation
Support embedding geolocation data
2 parents a6f2ff4 + 1a00022 commit 371e985

File tree

13 files changed

+80
-6
lines changed

13 files changed

+80
-6
lines changed

lib/csp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var defaultDirectives = {
77
defaultSrc: ['\'self\''],
88
scriptSrc: ['\'self\'', 'vimeo.com', 'https://gist.github.com', 'www.slideshare.net', 'https://query.yahooapis.com', '\'unsafe-eval\''],
99
// ^ TODO: Remove unsafe-eval - webpack script-loader issues https://github.com/hackmdio/codimd/issues/594
10-
imgSrc: ['*'],
10+
imgSrc: ['*', 'data:'],
1111
styleSrc: ['\'self\'', '\'unsafe-inline\'', 'https://github.githubassets.com'], // unsafe-inline is required for some libs, plus used in views
1212
fontSrc: ['\'self\'', 'data:', 'https://public.slidesharecdn.com'],
1313
objectSrc: ['*'], // Chrome PDF viewer treats PDFs as objects :/

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
"imports-loader": "~0.8.0",
178178
"intelli-espower-loader": "~1.0.1",
179179
"jsonlint": "~1.6.2",
180+
"leaflet": "~1.6.0",
180181
"less": "~3.9.0",
181182
"less-loader": "~4.1.0",
182183
"markdown-it-ruby": "^0.1.1",

public/css/extra.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@
7979
width: 100%;
8080
height: 100%;
8181
}
82+
.geo-map {
83+
width: 100%;
84+
height: 250px;
85+
}
8286

8387
.MJX_Assistive_MathML {
8488
display: none;

public/css/markdown.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
.markdown-body pre.graphviz,
128128
.markdown-body pre.mermaid,
129129
.markdown-body pre.abc,
130+
.markdown-body pre.geo,
130131
.markdown-body pre.vega {
131132
text-align: center;
132133
background-color: inherit;

public/css/slide.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ pre.sequence-diagram,
224224
pre.graphviz,
225225
pre.mermaid,
226226
pre.abc,
227+
pre.geo,
227228
pre.vega {
228229
text-align: center;
229230
background-color: white;

public/js/extra.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-env browser, jquery */
2-
/* global moment, serverurl, plantumlServer */
2+
/* global moment, serverurl, plantumlServer, L */
33

44
import Prism from 'prismjs'
55
import hljs from 'highlight.js'
@@ -453,6 +453,48 @@ export function finishView (view) {
453453
console.warn(err)
454454
}
455455
})
456+
// geo map
457+
view.find('div.geo.raw').removeClass('raw').each(async function (key, value) {
458+
const $elem = $(value).parent().parent()
459+
const $value = $(value)
460+
const content = $value.text()
461+
$value.unwrap()
462+
463+
try {
464+
let position, zoom
465+
if (content.match(/^[\d.,\s]+$/)) {
466+
const [lng, lat, zoo] = content.split(',').map(parseFloat)
467+
zoom = zoo
468+
position = [lat, lng]
469+
} else {
470+
// parse value as address
471+
const data = await fetch(`https://nominatim.openstreetmap.org/search?q=${encodeURIComponent(content)}&format=json`).then(r => r.json())
472+
if (!data || !data.length) {
473+
throw new Error('Location not found')
474+
}
475+
const { lat, lon } = data[0]
476+
position = [lat, lon]
477+
}
478+
$elem.html(`<div class="geo-map"></div>`)
479+
const map = L.map($elem.find('.geo-map')[0]).setView(position, zoom || 16)
480+
481+
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
482+
attribution: '<a href="https://www.openstreetmap.org/">OSM</a>',
483+
maxZoom: 18
484+
}).addTo(map)
485+
L.marker(position, {
486+
icon: L.icon({
487+
iconUrl: `${serverurl}/build/leaflet/images/marker-icon.png`,
488+
shadowUrl: `${serverurl}/build/leaflet/images/marker-shadow.png`
489+
})
490+
}).addTo(map)
491+
$elem.addClass('geo')
492+
} catch (err) {
493+
$elem.append(`<div class="alert alert-warning">${escapeHTML(err)}</div>`)
494+
console.warn(err)
495+
}
496+
})
497+
456498
// image href new window(emoji not included)
457499
const images = view.find('img.raw[src]').removeClass('raw')
458500
images.each((key, value) => {
@@ -1013,6 +1055,8 @@ function highlightRender (code, lang) {
10131055
return `<div class="abc raw">${code}</div>`
10141056
} else if (lang === 'vega') {
10151057
return `<div class="vega raw">${code}</div>`
1058+
} else if (lang === 'geo') {
1059+
return `<div class="geo raw">${code}</div>`
10161060
}
10171061
const result = {
10181062
value: code

public/js/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ var cursorActivityDebounce = 50
101101
var cursorAnimatePeriod = 100
102102
var supportContainers = ['success', 'info', 'warning', 'danger', 'spoiler']
103103
var supportCodeModes = ['javascript', 'typescript', 'jsx', 'htmlmixed', 'htmlembedded', 'css', 'xml', 'clike', 'clojure', 'ruby', 'python', 'shell', 'php', 'sql', 'haskell', 'coffeescript', 'yaml', 'pug', 'lua', 'cmake', 'nginx', 'perl', 'sass', 'r', 'dockerfile', 'tiddlywiki', 'mediawiki', 'go', 'gherkin'].concat(hljs.listLanguages())
104-
var supportCharts = ['sequence', 'flow', 'graphviz', 'mermaid', 'abc', 'plantuml', 'vega']
104+
var supportCharts = ['sequence', 'flow', 'graphviz', 'mermaid', 'abc', 'plantuml', 'vega', 'geo']
105105
var supportHeaders = [
106106
{
107107
text: '# h1',

public/views/codimd/foot.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega/5.9.1/vega.min.js" integrity="sha256-xVmd2OiOTh73s2iPfGy1DNyu/lCKvaDto452MU1O+xs=" crossorigin="anonymous" defer></script>
2525
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega-lite/4.4.0/vega-lite.min.js" integrity="sha256-ollz/GSuG0/f7aV4v8LGDYxPs4G2DwEk9+hALicqp9I=" crossorigin="anonymous" defer></script>
2626
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega-embed/6.2.2/vega-embed.min.js" integrity="sha256-AW13lGYqQzWT9PymwqUEJqQHaz9ntM5m5jQVkvtzja4=" crossorigin="anonymous" defer></script>
27+
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.6.0/leaflet.js" integrity="sha256-fNoRrwkP2GuYPbNSJmMJOCyfRB2DhPQe0rGTgzRsyso=" crossorigin="anonymous" defer></script>
2728
<%- include ../build/index-scripts %>
2829
<% } else { %>
2930
<script src="<%- serverURL %>/build/MathJax/MathJax.js" defer></script>

public/views/codimd/head.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-social/4.9.0/bootstrap-social.min.css" integrity="sha256-02JtFTurpwBjQJ6q13iJe82/NF0RbZlJroDegK5g87Y=" crossorigin="anonymous" />
1414
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css" integrity="sha256-3iu9jgsy9TpTwXKb7bNQzqWekRX7pPK+2OLj3R922fo=" crossorigin="anonymous" />
1515
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@hackmd/emojify.js@2.1.0/dist/css/basic/emojify.min.css" integrity="sha256-UOrvMOsSDSrW6szVLe8ZDZezBxh5IoIfgTwdNDgTjiU=" crossorigin="anonymous" />
16+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.6.0/leaflet.css" integrity="sha256-SHMGCYmST46SoyGgo4YR/9AlK1vf3ff84Aq9yK4hdqM=" crossorigin="anonymous" />
1617
<%- include ../build/index-header %>
1718
<%- include ../shared/polyfill %>
1819
<% } else { %>

0 commit comments

Comments
 (0)