html -> md
All checks were successful
Release / release (push) Successful in 3m16s

This commit is contained in:
d.a.kolesov 2026-11-11 15:14:40 +07:00
parent 5d154dc904
commit 2c1a5439e8
8 changed files with 132 additions and 31 deletions

41
package-lock.json generated
View file

@ -20,15 +20,18 @@
"imapflow": "^1.0.171", "imapflow": "^1.0.171",
"jsdom": "^29.0.1", "jsdom": "^29.0.1",
"mailparser": "^3.9.6", "mailparser": "^3.9.6",
"marked": "^18.2.0",
"nodemailer": "^8.0.4", "nodemailer": "^8.0.4",
"pinia": "^3.0.2", "pinia": "^3.0.2",
"sanitize-html": "^2.17.2", "sanitize-html": "^2.17.2",
"turndown": "^7.2.5",
"vue": "^3.5.13", "vue": "^3.5.13",
"vue-router": "^4.5.1" "vue-router": "^4.5.1"
}, },
"devDependencies": { "devDependencies": {
"@types/dompurify": "^3.2.0", "@types/dompurify": "^3.2.0",
"@types/nodemailer": "^8.0.0", "@types/nodemailer": "^8.0.0",
"@types/turndown": "^5.0.6",
"@vitejs/plugin-vue": "^5.2.3", "@vitejs/plugin-vue": "^5.2.3",
"electron": "^35.1.2", "electron": "^35.1.2",
"electron-builder": "^25.1.8", "electron-builder": "^25.1.8",
@ -1590,6 +1593,12 @@
"integrity": "sha512-dlq6VExJReb8dhjj3a/yTigr3ncNwoFmL5Iy2ENtbDX03EmNeOEdZ+vsaGrj7RTuO+mB7L58II4LCsl4NpM8uw==", "integrity": "sha512-dlq6VExJReb8dhjj3a/yTigr3ncNwoFmL5Iy2ENtbDX03EmNeOEdZ+vsaGrj7RTuO+mB7L58II4LCsl4NpM8uw==",
"license": "Apache-2.0" "license": "Apache-2.0"
}, },
"node_modules/@mixmark-io/domino": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/@mixmark-io/domino/-/domino-2.2.0.tgz",
"integrity": "sha512-Y28PR25bHXUg88kCV7nivXrP2Nj2RueZ3/l/jdx6J9f8J4nsEGcgX0Qe6lt7Pa+J79+kPiJU3LguR6O/6zrLOw==",
"license": "BSD-2-Clause"
},
"node_modules/@npmcli/fs": { "node_modules/@npmcli/fs": {
"version": "2.1.2", "version": "2.1.2",
"resolved": "https://registry.npmjs.org/@npmcli/fs/-/fs-2.1.2.tgz", "resolved": "https://registry.npmjs.org/@npmcli/fs/-/fs-2.1.2.tgz",
@ -2216,6 +2225,13 @@
"license": "MIT", "license": "MIT",
"optional": true "optional": true
}, },
"node_modules/@types/turndown": {
"version": "5.0.6",
"resolved": "https://registry.npmjs.org/@types/turndown/-/turndown-5.0.6.tgz",
"integrity": "sha512-ru00MoyeeouE5BX4gRL+6m/BsDfbRayOskWqUvh7CLGW+UXxHQItqALa38kKnOiZPqJrtzJUgAC2+F0rL1S4Pg==",
"dev": true,
"license": "MIT"
},
"node_modules/@types/verror": { "node_modules/@types/verror": {
"version": "1.10.11", "version": "1.10.11",
"resolved": "https://registry.npmjs.org/@types/verror/-/verror-1.10.11.tgz", "resolved": "https://registry.npmjs.org/@types/verror/-/verror-1.10.11.tgz",
@ -6375,6 +6391,18 @@
"node": ">=12" "node": ">=12"
} }
}, },
"node_modules/marked": {
"version": "18.2.0",
"resolved": "https://registry.npmjs.org/marked/-/marked-18.2.0.tgz",
"integrity": "sha512-KaeL+tAbtvI930q7HE2PN5lYXYdT8EiSmxHlN6F3rsfb57wd5mp5W5TLazpdj4inTQBJdgjYwbs40W/0VUGU+Q==",
"license": "MIT",
"bin": {
"marked": "bin/marked.js"
},
"engines": {
"node": ">= 20"
}
},
"node_modules/matcher": { "node_modules/matcher": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/matcher/-/matcher-3.0.0.tgz", "resolved": "https://registry.npmjs.org/matcher/-/matcher-3.0.0.tgz",
@ -8301,6 +8329,19 @@
"utf8-byte-length": "^1.0.1" "utf8-byte-length": "^1.0.1"
} }
}, },
"node_modules/turndown": {
"version": "7.2.5",
"resolved": "https://registry.npmjs.org/turndown/-/turndown-7.2.5.tgz",
"integrity": "sha512-EKZvCCFHh8sWttpAMs10U6niZFJ2cpz8LkCPlylxoG4JcHF50J7EHnnBW3AY1qXtUMu7kDjCp7dnggB1sIbqEQ==",
"license": "MIT",
"dependencies": {
"@mixmark-io/domino": "^2.2.0"
},
"engines": {
"node": ">=18",
"npm": ">=9"
}
},
"node_modules/type-fest": { "node_modules/type-fest": {
"version": "2.19.0", "version": "2.19.0",
"resolved": "https://registry.npmjs.org/type-fest/-/type-fest-2.19.0.tgz", "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-2.19.0.tgz",

View file

@ -32,15 +32,18 @@
"imapflow": "^1.0.171", "imapflow": "^1.0.171",
"jsdom": "^29.0.1", "jsdom": "^29.0.1",
"mailparser": "^3.9.6", "mailparser": "^3.9.6",
"marked": "^18.2.0",
"nodemailer": "^8.0.4", "nodemailer": "^8.0.4",
"pinia": "^3.0.2", "pinia": "^3.0.2",
"sanitize-html": "^2.17.2", "sanitize-html": "^2.17.2",
"turndown": "^7.2.5",
"vue": "^3.5.13", "vue": "^3.5.13",
"vue-router": "^4.5.1" "vue-router": "^4.5.1"
}, },
"devDependencies": { "devDependencies": {
"@types/dompurify": "^3.2.0", "@types/dompurify": "^3.2.0",
"@types/nodemailer": "^8.0.0", "@types/nodemailer": "^8.0.0",
"@types/turndown": "^5.0.6",
"@vitejs/plugin-vue": "^5.2.3", "@vitejs/plugin-vue": "^5.2.3",
"electron": "^35.1.2", "electron": "^35.1.2",
"electron-builder": "^25.1.8", "electron-builder": "^25.1.8",

View file

@ -0,0 +1,36 @@
import TurndownService from 'turndown'
const turndown = new TurndownService({
headingStyle: 'atx',
bulletListMarker: '-',
codeBlockStyle: 'fenced',
emDelimiter: '*'
})
turndown.remove(['style', 'script', 'head', 'title', 'meta'])
function decodeXmlText(raw: string): string {
const cdata = raw.match(/^\s*<!\[CDATA\[([\s\S]*?)\]\]>\s*$/)
if (cdata) return cdata[1]
return raw
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
.replace(/&apos;/g, "'")
.replace(/&#(\d+);/g, (_, n) => String.fromCodePoint(parseInt(n, 10)))
.replace(/&#x([0-9a-f]+);/gi, (_, n) => String.fromCodePoint(parseInt(n, 16)))
.replace(/&amp;/g, '&')
}
/**
* Преобразует содержимое `<t:Body>` из EWS (XML-экранированный или CDATA HTML, либо plain text)
* в Markdown.
*/
export function ewsBodyToMarkdown(rawBody: string, bodyType?: string): string {
const content = decodeXmlText(rawBody)
const isHtml = bodyType ? bodyType.toLowerCase() === 'html' : /<\/?[a-z][^>]*>/i.test(content)
const markdown = isHtml ? turndown.turndown(content) : content
return markdown
.replace(/ /g, ' ')
.replace(/\n{3,}/g, '\n\n')
.trim()
}

View file

@ -11,6 +11,7 @@ import http from 'node:http'
import type { IncomingMessage } from 'node:http' import type { IncomingMessage } from 'node:http'
import type { AccountConfig, ImapCredentials } from '@shared/types/account' import type { AccountConfig, ImapCredentials } from '@shared/types/account'
import type { CalendarInfo, CalendarEvent, AttendeeInfo, AttendeeStatus } from '@shared/types/calendar' import type { CalendarInfo, CalendarEvent, AttendeeInfo, AttendeeStatus } from '@shared/types/calendar'
import { ewsBodyToMarkdown } from '../lib/html-to-markdown'
import { import {
buildNegotiateHeader, buildNegotiateHeader,
parseChallenge, parseChallenge,
@ -264,22 +265,11 @@ export class CalDavProvider {
return distinguishedId === 'calendar' ? 'Календарь' : distinguishedId return distinguishedId === 'calendar' ? 'Календарь' : distinguishedId
} }
private static htmlToText(html: string): string { private static parseEwsBody(block: string): string | undefined {
return html const r = block.match(/<t:Body(?:\s([^>]*))?>([\s\S]*?)<\/t:Body>/)
.replace(/<br\s*\/?>/gi, '\n') if (!r) return undefined
.replace(/<\/p\s*>/gi, '\n\n') const bodyType = r[1]?.match(/BodyType="([^"]+)"/)?.[1]
.replace(/<\/div\s*>/gi, '\n') return ewsBodyToMarkdown(r[2], bodyType) || undefined
.replace(/<\/li\s*>/gi, '\n')
.replace(/<\/tr\s*>/gi, '\n')
.replace(/<[^>]+>/g, '')
.replace(/&nbsp;/g, ' ')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&amp;/g, '&')
.replace(/&quot;/g, '"')
.replace(/&#(\d+);/g, (_, n) => String.fromCharCode(parseInt(n, 10)))
.replace(/\n{3,}/g, '\n\n')
.trim()
} }
private async enrichWithAttendees(events: CalendarEvent[]): Promise<void> { private async enrichWithAttendees(events: CalendarEvent[]): Promise<void> {
@ -311,8 +301,7 @@ export class CalDavProvider {
if (target) { if (target) {
target.attendees = this.parseAttendeesList(itemBlock) target.attendees = this.parseAttendeesList(itemBlock)
if (!target.description) { if (!target.description) {
const body = itemBlock.match(/<t:Body(?:\s[^>]*)?>([\s\S]*?)<\/t:Body>/) target.description = CalDavProvider.parseEwsBody(itemBlock)
target.description = (body && CalDavProvider.htmlToText(body[1])) || undefined
} }
} }
} }
@ -379,12 +368,6 @@ export class CalDavProvider {
const r = block.match(new RegExp(`<t:${tag}(?:\\s[^>]*)?>([\\s\\S]*?)<\\/t:${tag}>`)) const r = block.match(new RegExp(`<t:${tag}(?:\\s[^>]*)?>([\\s\\S]*?)<\\/t:${tag}>`))
return r ? r[1].replace(/<[^>]+>/g, '').trim() : '' return r ? r[1].replace(/<[^>]+>/g, '').trim() : ''
} }
const getBody = (): string | undefined => {
const r = block.match(/<t:Body(?:\s[^>]*)?>([\s\S]*?)<\/t:Body>/)
if (!r) return undefined
const text = CalDavProvider.htmlToText(r[1])
return text || undefined
}
const idM = block.match(/<t:ItemId\s+Id="([^"]+)"/) const idM = block.match(/<t:ItemId\s+Id="([^"]+)"/)
if (!idM) continue if (!idM) continue
const startStr = get('Start') const startStr = get('Start')
@ -421,7 +404,7 @@ export class CalDavProvider {
end: end.toISOString(), end: end.toISOString(),
isAllDay, isAllDay,
location: get('Location') || undefined, location: get('Location') || undefined,
description: getBody(), description: CalDavProvider.parseEwsBody(block),
organizer, organizer,
attendees: [], attendees: [],
status: fbStatus === 'tentative' ? 'tentative' : 'confirmed' status: fbStatus === 'tentative' ? 'tentative' : 'confirmed'

View file

@ -2,6 +2,7 @@
import { computed, ref } from 'vue' import { computed, ref } from 'vue'
import { format } from 'date-fns' import { format } from 'date-fns'
import { ru } from 'date-fns/locale' import { ru } from 'date-fns/locale'
import { renderMarkdown } from '../../composables/useMarkdown'
import type { CalendarEvent, AttendeeInfo } from '@shared/types/calendar' import type { CalendarEvent, AttendeeInfo } from '@shared/types/calendar'
import AppIcon from '../AppIcon.vue' import AppIcon from '../AppIcon.vue'
import { useIpc } from '../../composables/useIpc' import { useIpc } from '../../composables/useIpc'
@ -276,7 +277,8 @@ async function copyEmail(email: string) {
<!-- Description --> <!-- Description -->
<div v-if="event.description" class="card desc-card"> <div v-if="event.description" class="card desc-card">
<p class="desc-text selectable">{{ event.description }}</p> <!-- eslint-disable-next-line vue/no-v-html -->
<div class="desc-text markdown selectable" v-html="renderMarkdown(event.description)" />
</div> </div>
</div> </div>
@ -554,9 +556,21 @@ async function copyEmail(email: string) {
.desc-card { padding:16px } .desc-card { padding:16px }
.desc-text { .desc-text {
font-size:13px; color:rgba(255,255,255,0.52); white-space:pre-wrap; font-size:13px; color:rgba(255,255,255,0.52);
line-height:1.7; margin:0; word-break:break-word; line-height:1.7; margin:0; word-break:break-word;
} }
.desc-text :deep(> :first-child) { margin-top:0 }
.desc-text :deep(> :last-child) { margin-bottom:0 }
.desc-text :deep(p), .desc-text :deep(ul), .desc-text :deep(ol), .desc-text :deep(pre) { margin:0 0 8px }
.desc-text :deep(ul), .desc-text :deep(ol) { padding-left:20px }
.desc-text :deep(h1), .desc-text :deep(h2), .desc-text :deep(h3), .desc-text :deep(h4) { font-size:13px; margin:8px 0 4px; color:rgba(255,255,255,0.7) }
.desc-text :deep(a) { color:var(--accent); text-decoration:underline }
.desc-text :deep(code) { font-family:monospace; background:rgba(255,255,255,0.08); padding:0 4px; border-radius:4px }
.desc-text :deep(pre) { overflow-x:auto }
.desc-text :deep(blockquote) { margin:0 0 8px; padding-left:10px; border-left:2px solid rgba(255,255,255,0.2) }
.desc-text :deep(table) { border-collapse:collapse }
.desc-text :deep(td), .desc-text :deep(th) { border:1px solid rgba(255,255,255,0.15); padding:2px 6px }
.desc-text :deep(img) { max-width:100% }
/* ── Material 3 ── */ /* ── Material 3 ── */
/* Центрированное модальное окно → dialog-паттерн, extra-large shape, elevation-3 */ /* Центрированное модальное окно → dialog-паттерн, extra-large shape, elevation-3 */

View file

@ -0,0 +1,15 @@
import DOMPurify from 'dompurify'
import { marked } from 'marked'
DOMPurify.addHook('afterSanitizeAttributes', (node) => {
if (node.tagName === 'A') {
node.setAttribute('target', '_blank')
node.setAttribute('rel', 'noopener noreferrer')
}
})
/** Рендерит Markdown в безопасный HTML (для `v-html`). */
export function renderMarkdown(markdown: string): string {
const html = marked.parse(markdown, { async: false, gfm: true, breaks: true })
return DOMPurify.sanitize(html, { USE_PROFILES: { html: true } })
}

View file

@ -3,6 +3,7 @@ import { ref, onMounted, onUnmounted, watch, nextTick } from 'vue'
import { format } from 'date-fns' import { format } from 'date-fns'
import { useIpc } from '../composables/useIpc' import { useIpc } from '../composables/useIpc'
import { useSettingsStore } from '../stores/settings.store' import { useSettingsStore } from '../stores/settings.store'
import { renderMarkdown } from '../composables/useMarkdown'
import AppIcon from '../components/AppIcon.vue' import AppIcon from '../components/AppIcon.vue'
import type { MeetingReminder } from '@shared/types/calendar' import type { MeetingReminder } from '@shared/types/calendar'
@ -86,9 +87,12 @@ function open(key: string): void { void invoke('reminder:open', { key }) }
<span>{{ reminder.event.location }}</span> <span>{{ reminder.event.location }}</span>
</div> </div>
<div v-if="reminder.event.description" class="card-description"> <!-- eslint-disable-next-line vue/no-v-html -->
{{ reminder.event.description }} <div
</div> v-if="reminder.event.description"
class="card-description markdown"
v-html="renderMarkdown(reminder.event.description)"
/>
<div class="card-actions"> <div class="card-actions">
<button v-if="reminder.joinUrl" class="action-btn primary" @click="join(reminder.key)"> <button v-if="reminder.joinUrl" class="action-btn primary" @click="join(reminder.key)">
@ -208,7 +212,6 @@ function open(key: string): void { void invoke('reminder:open', { key }) }
.card-description { .card-description {
font-size: 12px; font-size: 12px;
color: var(--text-secondary); color: var(--text-secondary);
white-space: pre-line;
word-break: break-word; word-break: break-word;
display: -webkit-box; display: -webkit-box;
-webkit-line-clamp: 4; -webkit-line-clamp: 4;
@ -216,6 +219,11 @@ function open(key: string): void { void invoke('reminder:open', { key }) }
overflow: hidden; overflow: hidden;
} }
.card-description :deep(*) { margin: 0; padding: 0; font-size: inherit; }
.card-description :deep(ul),
.card-description :deep(ol) { padding-left: 16px; }
.card-description :deep(a) { color: var(--accent); }
.card-actions { .card-actions {
display: flex; display: flex;
gap: 6px; gap: 6px;

View file

@ -24,6 +24,7 @@ export interface CalendarEvent {
end: string // ISO 8601 end: string // ISO 8601
isAllDay: boolean isAllDay: boolean
location?: string location?: string
/** Описание в формате Markdown */
description?: string description?: string
organizer?: string organizer?: string
attendees: AttendeeInfo[] attendees: AttendeeInfo[]