fix clickable HTML links on Windows (#3015) (#3033)

This commit is contained in:
Felix Häusler 2022-02-20 13:52:37 +01:00 committed by GitHub
parent 34e4a22857
commit dd14385cb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 6 deletions

View File

@ -415,15 +415,20 @@ ipcMain.on('mt::format-link-click', (e, { data, dirname }) => {
return
}
const href = data.href || data.text
if (URL_REG.test(href)) {
shell.openExternal(href)
const urlCandidate = data.href || data.text
if (URL_REG.test(urlCandidate)) {
shell.openExternal(urlCandidate)
return
} else if (/^[a-z0-9]+:\/\//i.test(href)) {
} else if (/^[a-z0-9]+:\/\//i.test(urlCandidate)) {
// Prevent other URLs.
return
}
const href = data.href
if (!href) {
return
}
let pathname = null
if (path.isAbsolute(href)) {
pathname = href
@ -432,6 +437,7 @@ ipcMain.on('mt::format-link-click', (e, { data, dirname }) => {
}
if (pathname) {
pathname = path.normalize(pathname)
if (isMarkdownFile(pathname)) {
const win = BrowserWindow.fromWebContents(e.sender)
openFileOrFolder(win, pathname)

View File

@ -0,0 +1,24 @@
// __MARKTEXT_ONLY__
const MARKDOWN_EXTENSIONS = Object.freeze([
'markdown',
'mdown',
'mkdn',
'md',
'mkd',
'mdwn',
'mdtxt',
'mdtext',
'text',
'txt'
])
/**
* Returns true if the filename matches one of the markdown extensions allowed in MarkText.
*
* @param {string} filename Path or filename
*/
export const hasMarkdownExtension = filename => {
if (!filename || typeof filename !== 'string') return false
return MARKDOWN_EXTENSIONS.some(ext => filename.toLowerCase().endsWith(`.${ext}`))
}

View File

@ -1,8 +1,22 @@
import { isValidAttribute } from '../utils/dompurify'
import { isWin } from '../config' // __MARKTEXT_PATCH__
import { hasMarkdownExtension } from './markdownFile' // __MARKTEXT_PATCH__
export const sanitizeHyperlink = rawLink => {
if (rawLink && typeof rawLink === 'string' && isValidAttribute('a', 'href', rawLink)) {
if (rawLink && typeof rawLink === 'string') {
if (isValidAttribute('a', 'href', rawLink)) {
return rawLink
}
// __MARKTEXT_PATCH__
if (isWin && /^[a-zA-Z]:[/\\].+/.test(rawLink) && hasMarkdownExtension(rawLink)) {
// Create and try UNC path on Windows because "C:\file.md" isn't allowed.
const uncPath = `\\\\?\\${rawLink}`
if (isValidAttribute('a', 'href', uncPath)) {
return uncPath
}
}
// END __MARKTEXT_PATCH__
}
return ''
}