diff --git a/package-lock.json b/package-lock.json index 4c6b457..5177f86 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "name": "tdnet-digest", "version": "0.4.2", "dependencies": { + "marked": "^17.0.4", "pdfjs-dist": "^5.4.394", "react": "^18.3.1", "react-dom": "^18.3.1" @@ -5128,6 +5129,18 @@ "@jridgewell/sourcemap-codec": "^1.5.5" } }, + "node_modules/marked": { + "version": "17.0.4", + "resolved": "https://registry.npmjs.org/marked/-/marked-17.0.4.tgz", + "integrity": "sha512-NOmVMM+KAokHMvjWmC5N/ZOvgmSWuqJB8FoYI019j4ogb/PeRMKoKIjReZ2w3376kkA8dSJIP8uD993Kxc0iRQ==", + "license": "MIT", + "bin": { + "marked": "bin/marked.js" + }, + "engines": { + "node": ">= 20" + } + }, "node_modules/math-intrinsics": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", diff --git a/package.json b/package.json index 6187afd..17578eb 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "type-check": "tsc --noEmit" }, "dependencies": { + "marked": "^17.0.4", "pdfjs-dist": "^5.4.394", "react": "^18.3.1", "react-dom": "^18.3.1" diff --git a/src/content/constants/styles.ts b/src/content/constants/styles.ts index 0b2e619..6cc3b19 100644 --- a/src/content/constants/styles.ts +++ b/src/content/constants/styles.ts @@ -30,7 +30,7 @@ export const SUMMARY_STYLES = { 'font-size: 12px; color: #92400e; margin-bottom: 8px; padding: 8px; background-color: #fef3c7; border: 1px solid #fbbf24; border-radius: 4px;', // 要約テキスト - summaryText: 'font-size: 13px; color: #374151; line-height: 1.6; white-space: pre-wrap;', + summaryText: 'font-size: 13px; color: #374151; line-height: 1.6;', } as const; /** diff --git a/src/content/utils/markdownParser.ts b/src/content/utils/markdownParser.ts new file mode 100644 index 0000000..26206b1 --- /dev/null +++ b/src/content/utils/markdownParser.ts @@ -0,0 +1,172 @@ +/** + * Markdown → インラインスタイル付きHTML変換ユーティリティ + * Content Scriptではグローバル CSS を使えないため、 + * marked の出力に対してインラインスタイルを適用する + */ + +import { type Tokens, Marked } from 'marked'; + +/** + * HTML特殊文字をエスケープする + */ +function escapeHtml(text: string): string { + return text + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); +} + +/** + * URLが安全なスキームかどうかを検証する + */ +function isSafeUrl(href: string): boolean { + try { + const url = new URL(href, 'https://example.com'); + return url.protocol === 'http:' || url.protocol === 'https:'; + } catch { + return false; + } +} + +/** 要素ごとのインラインスタイル定義 */ +const MARKDOWN_STYLES: Record = { + h1: 'font-size: 16px; font-weight: bold; color: #111827; margin: 12px 0 6px 0; padding-bottom: 4px; border-bottom: 1px solid #e5e7eb;', + h2: 'font-size: 15px; font-weight: bold; color: #1f2937; margin: 10px 0 6px 0; padding-bottom: 3px; border-bottom: 1px solid #f3f4f6;', + h3: 'font-size: 14px; font-weight: bold; color: #1f2937; margin: 8px 0 4px 0;', + h4: 'font-size: 13px; font-weight: bold; color: #374151; margin: 6px 0 4px 0;', + h5: 'font-size: 13px; font-weight: bold; color: #374151; margin: 4px 0 2px 0;', + h6: 'font-size: 12px; font-weight: bold; color: #6b7280; margin: 4px 0 2px 0;', + p: 'margin: 4px 0; line-height: 1.6;', + ul: 'margin: 4px 0; padding-left: 20px; list-style-type: disc;', + ol: 'margin: 4px 0; padding-left: 20px; list-style-type: decimal;', + li: 'margin: 2px 0; line-height: 1.5;', + strong: 'font-weight: bold;', + em: 'font-style: italic;', + blockquote: + 'margin: 6px 0; padding: 6px 12px; border-left: 3px solid #d1d5db; color: #6b7280; background-color: #f9fafb;', + code: 'font-family: monospace; font-size: 12px; background-color: #f3f4f6; padding: 1px 4px; border-radius: 3px;', + pre: 'margin: 6px 0; padding: 8px; background-color: #f3f4f6; border-radius: 4px; overflow-x: auto;', + 'pre code': + 'font-family: monospace; font-size: 12px; background-color: transparent; padding: 0; border-radius: 0;', + table: + 'border-collapse: collapse; margin: 6px 0; font-size: 12px; width: 100%;', + th: 'border: 1px solid #d1d5db; padding: 4px 8px; background-color: #f3f4f6; font-weight: bold; text-align: left;', + td: 'border: 1px solid #d1d5db; padding: 4px 8px;', + hr: 'border: none; border-top: 1px solid #e5e7eb; margin: 8px 0;', + a: 'color: #2563eb; text-decoration: underline;', +}; + +/** + * marked のレンダラーをカスタマイズしてインラインスタイルを付与 + */ +function createStyledRenderer(): Partial { + return { + heading(token: Tokens.Heading) { + const tag = `h${token.depth}` as keyof typeof MARKDOWN_STYLES; + const text = this.parser.parseInline(token.tokens); + return `<${tag} style="${MARKDOWN_STYLES[tag] || ''}">${text}`; + }, + paragraph(token: Tokens.Paragraph) { + const text = this.parser.parseInline(token.tokens); + return `

${text}

`; + }, + list(token: Tokens.List) { + const tag = token.ordered ? 'ol' : 'ul'; + let body = ''; + for (const item of token.items) { + body += this.listitem(item); + } + return `<${tag} style="${MARKDOWN_STYLES[tag]}">${body}`; + }, + listitem(token: Tokens.ListItem) { + let text = ''; + if (token.tokens) { + text = this.parser.parse(token.tokens); + } + return `
  • ${text}
  • `; + }, + strong(token: Tokens.Strong) { + const text = this.parser.parseInline(token.tokens); + return `${text}`; + }, + em(token: Tokens.Em) { + const text = this.parser.parseInline(token.tokens); + return `${text}`; + }, + blockquote(token: Tokens.Blockquote) { + const body = this.parser.parse(token.tokens); + return `
    ${body}
    `; + }, + code(token: Tokens.Code) { + const langAttr = token.lang ? ` data-lang="${escapeHtml(token.lang)}"` : ''; + return `
    ${escapeHtml(token.text)}
    `; + }, + codespan(token: Tokens.Codespan) { + return `${escapeHtml(token.text)}`; + }, + table(token: Tokens.Table) { + let header = ''; + for (let i = 0; i < token.header.length; i++) { + const cell = token.header[i]; + const align = token.align[i]; + const alignStyle = align ? ` text-align: ${align};` : ''; + const text = this.parser.parseInline(cell.tokens); + header += `${text}`; + } + header += ''; + + let body = ''; + for (const row of token.rows) { + body += ''; + for (let i = 0; i < row.length; i++) { + const cell = row[i]; + const align = token.align[i]; + const alignStyle = align ? ` text-align: ${align};` : ''; + const text = this.parser.parseInline(cell.tokens); + body += `${text}`; + } + body += ''; + } + + return `${header}${body}
    `; + }, + tablerow(token: Tokens.TableRow) { + return `${token.text}`; + }, + tablecell(token: Tokens.TableCell) { + const tag = token.header ? 'th' : 'td'; + const text = this.parser.parseInline(token.tokens); + return `<${tag} style="${MARKDOWN_STYLES[tag]}">${text}`; + }, + hr() { + return `
    `; + }, + link(token: Tokens.Link) { + const text = this.parser.parseInline(token.tokens); + if (!isSafeUrl(token.href)) { + return text; + } + return `${text}`; + }, + }; +} + +/** marked インスタンス(シングルトン) */ +const markedInstance = new Marked({ + renderer: createStyledRenderer(), + gfm: true, + breaks: true, +}); + +/** + * Markdown テキストをインラインスタイル付き HTML に変換する + */ +export function parseMarkdown(markdown: string): string { + const result = markedInstance.parse(markdown); + if (typeof result !== 'string') { + return markdown; + } + return result; +} diff --git a/src/content/utils/summaryHtmlBuilder.ts b/src/content/utils/summaryHtmlBuilder.ts index 80e94f3..3377155 100644 --- a/src/content/utils/summaryHtmlBuilder.ts +++ b/src/content/utils/summaryHtmlBuilder.ts @@ -5,6 +5,7 @@ import { SUMMARY_STYLES } from '../constants/styles'; import type { SummaryMetadata } from '../types/summaryMetadata'; +import { parseMarkdown } from './markdownParser'; /** * エラー表示のHTMLを生成 @@ -70,7 +71,7 @@ export function buildSummaryHtml( ${metadataHtml} -
    ${summaryText}
    +
    ${parseMarkdown(summaryText)}
    `; }