Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions browser/components/markdown.styl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ body
padding 5px
border-radius 5px
justify-content left
.audio-player
width: 100%
margin-bottom: 1em
li
label.taskListItem
margin-left -1.8em
Expand Down
42 changes: 42 additions & 0 deletions browser/lib/markdown-it-audio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict'

module.exports = function audioPlugin (md) {
function audio (state, startLine) {
const audioSouceRegex = /^@\((.*?)\)/
const start = state.bMarks[startLine]
const end = state.eMarks[startLine]
let token = null

// if it's indented more than 3 spaces, it should be a code block
if (state.sCount[startLine] - state.blkIndent >= 4) { return false }

// Audio must be at start of input or the previous line must be blank.
if (startLine !== 0) {
const prevLineStartPos = state.bMarks[startLine - 1] + state.tShift[startLine - 1]
const prevLineMaxPos = state.eMarks[startLine - 1]
if (prevLineMaxPos > prevLineStartPos) return false
}

const match = audioSouceRegex.exec(state.src.slice(start, end))

if (!match || match.length < 2) {
return false
}
token = state.push('audio')
state.line = startLine + 1
const src = match[1]
token.src = src
return true
}

function audioRender (tokens, idx) {
const token = tokens[idx]
return `<audio class='audio-player' src='${token.src}' controls></audio>`
}

md.block.ruler.before('fence', 'audio', audio, {
alt: ['paragraph', 'reference', 'blockquote', 'list']
})

md.renderer.rules['audio'] = audioRender
}
1 change: 1 addition & 0 deletions browser/lib/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class Markdown {
this.md.use(require('markdown-it-kbd'))
this.md.use(require('markdown-it-admonition'))
this.md.use(require('./markdown-it-frontmatter'))
this.md.use(require('./markdown-it-audio'))

const deflate = require('markdown-it-plantuml/lib/deflate')
this.md.use(require('markdown-it-plantuml'), '', {
Expand Down
20 changes: 15 additions & 5 deletions browser/main/lib/dataApi/attachmentManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function migrateAttachments (markdownContent, storagePath, noteKey) {
* @returns {String} postprocessed HTML in which all :storage references are mapped to the actual paths.
*/
function fixLocalURLS (renderedHTML, storagePath) {
return renderedHTML.replace(new RegExp('/?' + STORAGE_FOLDER_PLACEHOLDER + '.*?"', 'g'), function (match) {
return renderedHTML.replace(new RegExp('/?' + STORAGE_FOLDER_PLACEHOLDER + '.*?["|\']', 'g'), function (match) {
var encodedPathSeparators = new RegExp(mdurl.encode(path.win32.sep) + '|' + mdurl.encode(path.posix.sep), 'g')
return match.replace(encodedPathSeparators, path.sep).replace(new RegExp('/?' + STORAGE_FOLDER_PLACEHOLDER, 'g'), 'file:///' + path.join(storagePath, DESTINATION_FOLDER))
})
Expand All @@ -120,8 +120,13 @@ function fixLocalURLS (renderedHTML, storagePath) {
* @param {Boolean} showPreview Indicator whether the generated markdown should show a preview of the image. Note that at the moment only previews for images are supported
* @returns {String} Generated markdown code
*/
function generateAttachmentMarkdown (fileName, path, showPreview) {
return `${showPreview ? '!' : ''}[${fileName}](${path})`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you remove the parameter (and evaluation) ´showPreview´????

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I'll fix it right away 😄

function generateAttachmentMarkdown (fileName, path, previewType) {
if (previewType === 'image') {
return `![${fileName}](${path})`
} else if (previewType === 'audio') {
return `@(${path})`
}
return `[${fileName}](${path})`
}

/**
Expand All @@ -139,8 +144,13 @@ function handleAttachmentDrop (codeEditor, storageKey, noteKey, dropEvent) {
const fileType = file['type']

copyAttachment(filePath, storageKey, noteKey).then((fileName) => {
const showPreview = fileType.startsWith('image')
const imageMd = generateAttachmentMarkdown(originalFileName, path.join(STORAGE_FOLDER_PLACEHOLDER, noteKey, fileName), showPreview)
let previewType = null
if (fileType.startsWith('image')) {
previewType = 'image'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you should use constants istead of strings for this!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

} else if (fileType.startsWith('audio')) {
previewType = 'audio'
}
const imageMd = generateAttachmentMarkdown(originalFileName, path.join(STORAGE_FOLDER_PLACEHOLDER, noteKey, fileName), previewType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you've modified the previewGeneration on this place as well!

codeEditor.insertAttachmentMd(imageMd)
})
}
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/markdowns.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ const smartQuotes = 'This is a "QUOTE".'

const breaks = 'This is the first line.\nThis is the second line.'

const audio = `
@(audio.mp3)

Audio must be at start of input or the previous line must be blank.
@(notaudio.mp3)
`

export default {
basic,
codeblock,
Expand Down
5 changes: 5 additions & 0 deletions tests/lib/markdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ test('Markdown.render() should render line breaks correctly', t => {
const renderedNonBreaks = newmd.render(markdownFixtures.breaks)
t.snapshot(renderedNonBreaks)
})

test('Markdown.render() should render audio correctly', t => {
const rendered = md.render(markdownFixtures.audio)
t.snapshot(rendered)
})
7 changes: 7 additions & 0 deletions tests/lib/snapshots/markdown-test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,10 @@ Generated by [AVA](https://ava.li).

`<p data-line="0">This is a &quot;QUOTE&quot;.</p>␊
`
## Markdown.render() should render audio correctly

> Snapshot 1

`<audio class='audio-player' src='audio.mp3' controls></audio>
Audio must be at start of input or the previous line must be blank.
@(notaudio.mp3)
Binary file modified tests/lib/snapshots/markdown-test.js.snap
Binary file not shown.
100 changes: 96 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ ansi-escapes@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-3.1.0.tgz#f73207bb81207d75fd6c83f125af26eea378ca30"

ansi-red@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/ansi-red/-/ansi-red-0.1.1.tgz#8c638f9d1080800a353c9c28c8a81ca4705d946c"
dependencies:
ansi-wrap "0.1.0"

ansi-regex@^0.2.0, ansi-regex@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-0.2.1.tgz#0d8e946967a3d8143f93e24e298525fc1b2235f9"
Expand Down Expand Up @@ -218,6 +224,10 @@ ansi-styles@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-1.0.0.tgz#cb102df1c56f5123eab8b67cd7b98027a0279178"

[email protected]:
version "0.1.0"
resolved "https://registry.yarnpkg.com/ansi-wrap/-/ansi-wrap-0.1.0.tgz#a82250ddb0015e9a27ca82e82ea603bbfa45efaf"

anymatch@^1.3.0:
version "1.3.2"
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-1.3.2.tgz#553dcb8f91e3c889845dfdba34c77721b90b9d7a"
Expand Down Expand Up @@ -255,7 +265,7 @@ argparse@^1.0.7, argparse@~1.0.3:
dependencies:
sprintf-js "~1.0.2"

"argparse@~ 0.1.11":
"argparse@~ 0.1.11", argparse@~0.1.15:
version "0.1.16"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-0.1.16.tgz#cfd01e0fbba3d6caed049fbd758d40f65196f57c"
dependencies:
Expand Down Expand Up @@ -459,6 +469,10 @@ auto-bind@^1.1.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/auto-bind/-/auto-bind-1.2.0.tgz#8b7e318aad53d43ba8a8ecaf0066d85d5f798cd6"

autolinker@~0.15.0:
version "0.15.3"
resolved "https://registry.yarnpkg.com/autolinker/-/autolinker-0.15.3.tgz#342417d8f2f3461b14cf09088d5edf8791dc9832"

autoprefixer@^6.3.1:
version "6.7.7"
resolved "https://registry.yarnpkg.com/autoprefixer/-/autoprefixer-6.7.7.tgz#1dbd1c835658e35ce3f9984099db00585c782014"
Expand Down Expand Up @@ -1793,7 +1807,7 @@ codemirror@^5.39.0:
version "5.39.0"
resolved "https://registry.yarnpkg.com/codemirror/-/codemirror-5.39.0.tgz#4654f7d2f7e525e04a62e72d9482348ccb37dce5"

coffee-script@^1.10.0:
coffee-script@^1.10.0, coffee-script@^1.12.4:
version "1.12.7"
resolved "https://registry.yarnpkg.com/coffee-script/-/coffee-script-1.12.7.tgz#c05dae0cb79591d05b3070a8433a98c9a89ccc53"

Expand Down Expand Up @@ -2666,6 +2680,10 @@ devtron@^1.1.0:
highlight.js "^9.3.0"
humanize-plus "^1.8.1"

diacritics-map@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/diacritics-map/-/diacritics-map-0.1.0.tgz#6dfc0ff9d01000a2edf2865371cac316e94977af"

diff@^3.2.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"
Expand Down Expand Up @@ -4030,6 +4048,16 @@ graphlibrary@^2.2.0:
dependencies:
lodash "^4.17.5"

gray-matter@^2.1.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/gray-matter/-/gray-matter-2.1.1.tgz#3042d9adec2a1ded6a7707a9ed2380f8a17a430e"
dependencies:
ansi-red "^0.1.1"
coffee-script "^1.12.4"
extend-shallow "^2.0.1"
js-yaml "^3.8.1"
toml "^2.3.2"

growly@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"
Expand Down Expand Up @@ -5254,6 +5282,13 @@ js-yaml@^3.10.0, js-yaml@^3.5.1, js-yaml@^3.7.0:
argparse "^1.0.7"
esprima "^4.0.0"

js-yaml@^3.8.1:
version "3.12.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.12.0.tgz#eaed656ec8344f10f527c6bfa1b6e2244de167d1"
dependencies:
argparse "^1.0.7"
esprima "^4.0.0"

js-yaml@~2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-2.0.5.tgz#a25ae6509999e97df278c6719da11bd0687743a8"
Expand Down Expand Up @@ -5487,6 +5522,12 @@ lazy-cache@^1.0.3:
version "1.0.4"
resolved "https://registry.yarnpkg.com/lazy-cache/-/lazy-cache-1.0.4.tgz#a1d78fc3a50474cb80845d3b3b6e1da49a446e8e"

lazy-cache@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/lazy-cache/-/lazy-cache-2.0.2.tgz#b9190a4f913354694840859f8a8f7084d8822264"
dependencies:
set-getter "^0.1.0"

lcid@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/lcid/-/lcid-1.0.0.tgz#308accafa0bc483a3867b4b6f2b9506251d1b835"
Expand Down Expand Up @@ -5514,6 +5555,15 @@ linkify-it@~1.2.0, linkify-it@~1.2.2:
dependencies:
uc.micro "^1.0.1"

list-item@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/list-item/-/list-item-1.1.1.tgz#0c65d00e287cb663ccb3cb3849a77e89ec268a56"
dependencies:
expand-range "^1.8.1"
extend-shallow "^2.0.1"
is-number "^2.1.0"
repeat-string "^1.5.2"

load-json-file@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/load-json-file/-/load-json-file-1.1.0.tgz#956905708d58b4bab4c2261b04f59f31c99374c0"
Expand Down Expand Up @@ -5783,6 +5833,27 @@ markdown-it@^6.0.1:
mdurl "~1.0.1"
uc.micro "^1.0.1"

markdown-link@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/markdown-link/-/markdown-link-0.1.1.tgz#32c5c65199a6457316322d1e4229d13407c8c7cf"

markdown-toc@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/markdown-toc/-/markdown-toc-1.2.0.tgz#44a15606844490314afc0444483f9e7b1122c339"
dependencies:
concat-stream "^1.5.2"
diacritics-map "^0.1.0"
gray-matter "^2.1.0"
lazy-cache "^2.0.2"
list-item "^1.1.1"
markdown-link "^0.1.1"
minimist "^1.2.0"
mixin-deep "^1.1.3"
object.pick "^1.2.0"
remarkable "^1.7.1"
repeat-string "^1.6.1"
strip-color "^0.1.0"

match-at@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/match-at/-/match-at-0.1.1.tgz#25d040d291777704d5e6556bbb79230ec2de0540"
Expand Down Expand Up @@ -6028,7 +6099,7 @@ minizlib@^1.1.0:
dependencies:
minipass "^2.2.1"

mixin-deep@^1.2.0:
mixin-deep@^1.1.3, mixin-deep@^1.2.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/mixin-deep/-/mixin-deep-1.3.1.tgz#a49e7268dce1a0d9698e45326c5626df3543d0fe"
dependencies:
Expand Down Expand Up @@ -6400,7 +6471,7 @@ object.omit@^2.0.0:
for-own "^0.1.4"
is-extendable "^0.1.1"

object.pick@^1.3.0:
object.pick@^1.2.0, object.pick@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/object.pick/-/object.pick-1.3.0.tgz#87a10ac4c1694bd2e1cbf53591a66141fb5dd747"
dependencies:
Expand Down Expand Up @@ -7544,6 +7615,13 @@ release-zalgo@^1.0.0:
dependencies:
es6-error "^4.0.1"

remarkable@^1.7.1:
version "1.7.1"
resolved "https://registry.yarnpkg.com/remarkable/-/remarkable-1.7.1.tgz#aaca4972100b66a642a63a1021ca4bac1be3bff6"
dependencies:
argparse "~0.1.15"
autolinker "~0.15.0"

remove-trailing-separator@^1.0.1:
version "1.1.0"
resolved "https://registry.yarnpkg.com/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz#c24bce2a283adad5bc3f58e0d48249b92379d8ef"
Expand Down Expand Up @@ -7879,6 +7957,12 @@ set-blocking@^2.0.0, set-blocking@~2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"

set-getter@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/set-getter/-/set-getter-0.1.0.tgz#d769c182c9d5a51f409145f2fba82e5e86e80376"
dependencies:
to-object-path "^0.3.0"

set-immediate-shim@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/set-immediate-shim/-/set-immediate-shim-1.0.1.tgz#4b2b1b27eb808a9f8dcc481a58e5e56f599f3f61"
Expand Down Expand Up @@ -8325,6 +8409,10 @@ strip-bom@^2.0.0:
dependencies:
is-utf8 "^0.2.0"

strip-color@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/strip-color/-/strip-color-0.1.0.tgz#106f65d3d3e6a2d9401cac0eb0ce8b8a702b4f7b"

strip-eof@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/strip-eof/-/strip-eof-1.0.0.tgz#bb43ff5598a6eb05d89b59fcd129c983313606bf"
Expand Down Expand Up @@ -8606,6 +8694,10 @@ toggle-selection@^1.0.3:
version "1.0.6"
resolved "https://registry.yarnpkg.com/toggle-selection/-/toggle-selection-1.0.6.tgz#6e45b1263f2017fa0acc7d89d78b15b8bf77da32"

toml@^2.3.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/toml/-/toml-2.3.3.tgz#8d683d729577cb286231dfc7a8affe58d31728fb"

[email protected]:
version "0.0.3"
resolved "https://registry.yarnpkg.com/touch/-/touch-0.0.3.tgz#51aef3d449571d4f287a5d87c9c8b49181a0db1d"
Expand Down