Files
MdToHtmlDashboard/MdToHtmlDashboard/Templates/HtmlTemplate.cs
T

776 lines
22 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace MdToHtmlDashboard.Templates;
/// <summary>
/// 完全自己完結型インタラクティブHTMLを生成するためのテンプレート定義クラス
/// </summary>
public static class HtmlTemplate
{
/// <summary>
/// HTMLドキュメント全体のテンプレート
/// </summary>
public static string Render(string title, string subtitle, string bodyContent, string tocHtml)
{
return $$"""
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{title}}</title>
<style>
{{GetCss()}}
</style>
</head>
<body>
<!-- ヘッダー -->
<header>
<div class="header-container">
<h1 class="header-title">
<span class="header-icon">📑</span>
<span id="docTitle">{{title}}</span>
</h1>
{{(string.IsNullOrWhiteSpace(subtitle) ? "" : $"<p class=\"header-subtitle\">{subtitle}</p>")}}
</div>
</header>
<!-- レイアウトコンテナ -->
<div class="main-wrapper">
<!-- 目次(サイドバー) -->
<aside class="toc-sidebar" id="tocSidebar">
<div class="toc-header">
<span class="toc-title">📋 目次</span>
<button class="toc-close-btn" onclick="toggleSidebar(false)">✕</button>
</div>
<nav class="toc-nav">
{{tocHtml}}
</nav>
</aside>
<!-- メインコンテンツ -->
<main class="content-area">
<!-- コントロールバー(検索・一括開閉・目次切替) -->
<div class="controls-card">
<div class="search-box">
<span class="search-icon">🔍</span>
<input type="text" id="globalSearch" class="search-input" placeholder="ページ内をリアルタイム検索(見出し・表・コード・本文)..." oninput="handleSearch()">
</div>
<div class="control-actions">
<button class="action-btn" onclick="toggleSidebar()">📑 目次表示</button>
<button class="action-btn" onclick="expandAllSections(true)">すべて展開</button>
<button class="action-btn" onclick="expandAllSections(false)">すべて折りたたむ</button>
</div>
</div>
<!-- 本文領域 -->
<article class="markdown-body" id="markdownBody">
{{bodyContent}}
</article>
</main>
</div>
<!-- トースト通知 -->
<div id="toast">クリップボードにコピーしました</div>
<!-- インタラクティブ制御スクリプト -->
<script>
{{GetJavaScript()}}
</script>
</body>
</html>
""";
}
/// <summary>
/// モダンで洗練された完全自己完結CSS
/// </summary>
private static string GetCss()
{
return """
:root {
--primary: #1e3a8a;
--primary-light: #2563eb;
--primary-bg: #eff6ff;
--text-main: #1f2937;
--text-muted: #4b5563;
--border-color: #e2e8f0;
--bg-card: #ffffff;
--bg-body: #f8fafc;
--sidebar-width: 290px;
--success: #10b981;
--success-bg: #ecfdf5;
--diff-add-bg: #e6ffed;
--diff-add-color: #15803d;
--diff-del-bg: #ffeef0;
--diff-del-color: #b91c1c;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", "Yu Gothic", "Meiryo", sans-serif;
background-color: var(--bg-body);
color: var(--text-main);
line-height: 1.68;
font-size: 14px;
}
/* ヘッダー */
header {
background: linear-gradient(135deg, #1e3a8a 0%, #1e40af 50%, #2563eb 100%);
color: #ffffff;
padding: 24px 32px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
position: sticky;
top: 0;
z-index: 100;
}
.header-container {
max-width: 1400px;
margin: 0 auto;
}
.header-title {
font-size: 22px;
font-weight: 700;
display: flex;
align-items: center;
gap: 10px;
}
.header-icon {
font-size: 24px;
}
.header-subtitle {
font-size: 13.5px;
opacity: 0.9;
margin-top: 4px;
}
/* メインレイアウト */
.main-wrapper {
max-width: 1400px;
margin: 20px auto;
padding: 0 24px;
display: flex;
gap: 24px;
position: relative;
}
/* 目次サイドバー */
.toc-sidebar {
width: var(--sidebar-width);
flex-shrink: 0;
background: var(--bg-card);
border: 1px solid var(--border-color);
border-radius: 10px;
padding: 20px;
height: calc(100vh - 140px);
position: sticky;
top: 100px;
overflow-y: auto;
box-shadow: 0 2px 8px rgba(0,0,0,0.03);
transition: all 0.3s ease;
}
.toc-sidebar.hidden {
display: none;
}
.toc-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 14px;
border-bottom: 2px solid var(--border-color);
padding-bottom: 8px;
}
.toc-title {
font-weight: 700;
font-size: 15px;
color: var(--primary);
}
.toc-close-btn {
background: none;
border: none;
font-size: 16px;
cursor: pointer;
color: var(--text-muted);
display: none;
}
.toc-nav ul {
list-style: none;
padding-left: 12px;
}
.toc-nav > ul {
padding-left: 0;
}
.toc-nav li {
margin: 6px 0;
}
.toc-nav a {
color: var(--text-muted);
text-decoration: none;
font-size: 13px;
display: block;
padding: 3px 6px;
border-radius: 4px;
transition: all 0.15s;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.toc-nav a:hover {
color: var(--primary-light);
background: var(--primary-bg);
text-decoration: underline;
}
/* メインコンテンツエリア */
.content-area {
flex: 1;
min-width: 0;
}
/* コントロールバー */
.controls-card {
background: var(--bg-card);
border: 1px solid var(--border-color);
border-radius: 10px;
padding: 14px 20px;
margin-bottom: 20px;
display: flex;
flex-wrap: wrap;
gap: 12px;
align-items: center;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.02);
}
.search-box {
flex: 1;
min-width: 280px;
position: relative;
}
.search-input {
width: 100%;
padding: 10px 14px 10px 38px;
border: 1px solid var(--border-color);
border-radius: 6px;
font-size: 13.5px;
outline: none;
transition: all 0.2s;
}
.search-input:focus {
border-color: var(--primary-light);
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}
.search-icon {
position: absolute;
left: 12px;
top: 50%;
transform: translateY(-50%);
color: var(--text-muted);
}
.control-actions {
display: flex;
gap: 8px;
}
.action-btn {
background: #ffffff;
border: 1px solid var(--border-color);
padding: 8px 14px;
border-radius: 6px;
font-size: 12.5px;
font-weight: 600;
color: var(--text-main);
cursor: pointer;
transition: all 0.15s;
}
.action-btn:hover {
background: #f1f5f9;
border-color: #cbd5e1;
}
/* Markdown 本文コンテナ */
.markdown-body {
display: flex;
flex-direction: column;
gap: 20px;
}
.doc-intro {
background: var(--bg-card);
border: 1px solid var(--border-color);
border-radius: 10px;
padding: 24px 32px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.03);
}
/* セクションアコーディオンカード(details/summary) */
details.doc-section {
background: var(--bg-card);
border: 1px solid var(--border-color);
border-radius: 10px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.03);
transition: border-color 0.2s;
}
details.doc-section[open] {
border-color: #cbd5e1;
}
summary.doc-section-header {
padding: 16px 24px;
background: #ffffff;
cursor: pointer;
user-select: none;
list-style: none;
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 1px solid transparent;
transition: background 0.15s;
}
summary.doc-section-header::-webkit-details-marker {
display: none;
}
summary.doc-section-header:hover {
background: #f8fafc;
}
details[open] > summary.doc-section-header {
background: #f1f5f9;
border-bottom: 1px solid var(--border-color);
}
summary.doc-section-header h2 {
margin: 0 !important;
padding: 0 !important;
border: none !important;
font-size: 18px;
font-weight: 700;
color: var(--primary);
scroll-margin-top: 100px;
}
/* 開閉バッジアイコン */
summary.doc-section-header::after {
content: "▼ 折りたたむ";
font-size: 12px;
font-weight: 600;
color: var(--text-muted);
padding: 4px 10px;
border-radius: 6px;
background: #ffffff;
border: 1px solid var(--border-color);
flex-shrink: 0;
margin-left: 12px;
}
details:not([open]) > summary.doc-section-header::after {
content: "▶ 展開する";
background: #f8fafc;
}
.doc-section-content {
padding: 24px 32px;
}
/* 見出しスタイル */
.markdown-body h1 {
font-size: 24px;
color: var(--primary);
border-bottom: 2px solid var(--border-color);
padding-bottom: 10px;
margin-bottom: 16px;
}
.markdown-body h3 {
font-size: 16px;
color: #1e40af;
margin-top: 24px;
margin-bottom: 12px;
border-left: 4px solid var(--primary-light);
padding-left: 10px;
scroll-margin-top: 100px;
}
.markdown-body h4 {
font-size: 14.5px;
color: #334155;
margin-top: 20px;
margin-bottom: 10px;
scroll-margin-top: 100px;
}
.markdown-body p {
margin-bottom: 14px;
}
.markdown-body ul,
.markdown-body ol {
padding-left: 24px;
margin-bottom: 16px;
}
.markdown-body li {
margin-bottom: 4px;
}
.markdown-body hr {
border: 0;
height: 1px;
background: var(--border-color);
margin: 24px 0;
}
.markdown-body blockquote {
border-left: 4px solid var(--primary-light);
background: #f8fafc;
padding: 12px 18px;
margin-bottom: 16px;
border-radius: 0 6px 6px 0;
color: #334155;
}
/* テーブル */
.table-container {
margin: 18px 0;
border: 1px solid var(--border-color);
border-radius: 8px;
overflow-x: auto;
background: #ffffff;
box-shadow: 0 1px 4px rgba(0,0,0,0.02);
}
table {
width: 100%;
border-collapse: collapse;
font-size: 13px;
text-align: left;
}
th {
background: #f1f5f9;
color: #1e293b;
font-weight: 700;
padding: 11px 14px;
border-bottom: 2px solid var(--border-color);
white-space: nowrap;
user-select: none;
cursor: pointer;
}
th:hover {
background: #e2e8f0;
}
td {
padding: 11px 14px;
border-bottom: 1px solid var(--border-color);
vertical-align: middle;
}
tr:nth-child(even) td {
background-color: #fcfdfe;
}
tr:hover td {
background-color: #f1f5f9;
}
/* コードブロック */
.code-container {
position: relative;
margin: 16px 0;
background: #1e293b;
border-radius: 8px;
overflow: hidden;
}
.code-header {
background: #0f172a;
color: #94a3b8;
padding: 6px 14px;
font-size: 11px;
display: flex;
justify-content: space-between;
align-items: center;
font-family: Consolas, monospace;
}
.code-copy-btn {
background: #334155;
color: #f1f5f9;
border: none;
padding: 2px 8px;
border-radius: 4px;
font-size: 11px;
cursor: pointer;
transition: all 0.15s;
}
.code-copy-btn:hover {
background: var(--primary-light);
}
pre {
padding: 14px 18px;
overflow-x: auto;
font-family: Consolas, Monaco, "Courier New", monospace;
font-size: 12.5px;
line-height: 1.5;
color: #e2e8f0;
}
code {
font-family: Consolas, Monaco, "Courier New", monospace;
background: #f1f5f9;
color: #0f172a;
padding: 2px 6px;
border-radius: 4px;
font-size: 88%;
}
pre code {
background: transparent;
color: inherit;
padding: 0;
font-size: inherit;
}
/* diff ハイライト */
.diff-add {
color: #4ade80;
background: rgba(74, 222, 128, 0.12);
display: block;
}
.diff-del {
color: #f87171;
background: rgba(248, 113, 113, 0.12);
display: block;
}
/* パスコピーバッジ */
.file-path-badge {
display: inline-flex;
align-items: center;
gap: 6px;
background: #f1f5f9;
border: 1px solid #cbd5e1;
padding: 2px 8px;
border-radius: 4px;
font-size: 12px;
font-family: Consolas, monospace;
margin: 2px 4px;
}
.badge-copy-btn {
background: #ffffff;
border: 1px solid #94a3b8;
padding: 1px 5px;
border-radius: 3px;
font-size: 10px;
cursor: pointer;
}
.badge-copy-btn:hover {
background: var(--primary-light);
color: #ffffff;
border-color: var(--primary-light);
}
/* トースト通知 */
#toast {
position: fixed;
bottom: 24px;
right: 24px;
background: #1e293b;
color: #ffffff;
padding: 12px 20px;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
font-size: 13px;
opacity: 0;
transform: translateY(20px);
transition: all 0.25s ease;
z-index: 9999;
pointer-events: none;
}
#toast.show {
opacity: 1;
transform: translateY(0);
}
/* レスポンシブ */
@media (max-width: 992px) {
.toc-sidebar {
position: fixed;
left: 0;
top: 0;
height: 100vh;
z-index: 1000;
box-shadow: 2px 0 16px rgba(0,0,0,0.15);
display: none;
}
.toc-sidebar.mobile-open {
display: block;
}
.toc-close-btn {
display: block;
}
.doc-section-content, .doc-intro {
padding: 18px 16px;
}
}
""";
}
/// <summary>
/// クライアント側で動作するVanilla JavaScript
/// </summary>
private static string GetJavaScript()
{
return """
// テーブルのソート機能
document.querySelectorAll('table').forEach(table => {
const headers = table.querySelectorAll('th');
headers.forEach((th, colIndex) => {
th.title = 'クリックでソート';
th.addEventListener('click', () => {
sortTable(table, colIndex);
});
});
});
function sortTable(table, colIndex) {
const tbody = table.querySelector('tbody') || table;
const rows = Array.from(tbody.querySelectorAll('tr')).filter(r => r.querySelectorAll('td').length > 0);
const isAsc = table.getAttribute('data-sort-col') != colIndex || table.getAttribute('data-sort-dir') === 'desc';
rows.sort((rowA, rowB) => {
const cellA = (rowA.children[colIndex]?.innerText || '').trim();
const cellB = (rowB.children[colIndex]?.innerText || '').trim();
const numA = parseFloat(cellA.replace(/[^0-9.-]+/g, ''));
const numB = parseFloat(cellB.replace(/[^0-9.-]+/g, ''));
if (!isNaN(numA) && !isNaN(numB) && cellA === numA.toString()) {
return isAsc ? numA - numB : numB - numA;
}
return isAsc ? cellA.localeCompare(cellB, 'ja') : cellB.localeCompare(cellA, 'ja');
});
rows.forEach(r => tbody.appendChild(r));
table.setAttribute('data-sort-col', colIndex);
table.setAttribute('data-sort-dir', isAsc ? 'asc' : 'desc');
// ヘッダーアイコン更新
table.querySelectorAll('th').forEach((th, i) => {
th.innerText = th.innerText.replace(/ [▲▼↕]$/, '');
if (i === colIndex) {
th.innerText += isAsc ? ' ▲' : ' ▼';
} else {
th.innerText += ' ↕';
}
});
}
// リアルタイムインクリメンタル検索
function handleSearch() {
const query = document.getElementById('globalSearch').value.toLowerCase().trim();
// テーブル行のフィルタリング
document.querySelectorAll('table tbody tr').forEach(row => {
if (row.querySelectorAll('td').length === 0) return;
const text = row.innerText.toLowerCase();
row.style.display = (query === '' || text.includes(query)) ? '' : 'none';
});
// セクション単位のフィルタリング(ヒットしたセクションは自動展開)
document.querySelectorAll('details.doc-section').forEach(sec => {
const text = sec.innerText.toLowerCase();
if (query === '') {
sec.style.display = '';
} else if (text.includes(query)) {
sec.style.display = '';
sec.open = true; // 検索ヒット時は自動展開
} else {
sec.style.display = 'none';
}
});
}
// サイドバー開閉
function toggleSidebar(forceState) {
const sidebar = document.getElementById('tocSidebar');
if (window.innerWidth <= 992) {
sidebar.classList.toggle('mobile-open', forceState);
} else {
sidebar.classList.toggle('hidden', forceState !== undefined ? !forceState : undefined);
}
}
// すべて展開 / 折りたたみ
function expandAllSections(expand) {
document.querySelectorAll('details.doc-section').forEach(d => {
d.open = expand;
});
}
// コピー機能
function copyText(text) {
navigator.clipboard.writeText(text).then(() => {
showToast('クリップボードにコピーしました: ' + text);
}).catch(() => {
showToast('コピーに失敗しました');
});
}
function copyPreCode(btn) {
const pre = btn.closest('.code-container').querySelector('pre');
if (pre) {
navigator.clipboard.writeText(pre.innerText).then(() => {
showToast('コードをコピーしました');
});
}
}
function showToast(msg) {
const toast = document.getElementById('toast');
toast.textContent = msg;
toast.classList.add('show');
setTimeout(() => {
toast.classList.remove('show');
}, 2500);
}
""";
}
}