Refactor to Notemark: minimalist markdown editor app
- Remove Blazor template pages and navigation; redesign layout for fullscreen editor with light/dark theme support - Add Editor.razor: markdown editor with live preview, auto-save, and share modal for readonly/editable links - Implement MarkdownService (Markdig + HtmlSanitizer), SessionService (cookie-based, file persistence), and ShareService (token-based sharing) - Configure DI and cookie policy in Program.cs; add Markdig and HtmlSanitizer NuGet packages - Include example session and share token data files - App is now a single-page markdown editor with file-based storage and sharing, no database
This commit is contained in:
@@ -1,19 +1,7 @@
|
||||
@inherits LayoutComponentBase
|
||||
|
||||
<div class="page">
|
||||
<div class="sidebar">
|
||||
<NavMenu />
|
||||
</div>
|
||||
|
||||
<main>
|
||||
<div class="top-row px-4">
|
||||
<a href="https://learn.microsoft.com/aspnet/core/" target="_blank">About</a>
|
||||
</div>
|
||||
|
||||
<article class="content px-4">
|
||||
<div class="notemark-app">
|
||||
@Body
|
||||
</article>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
<div id="blazor-error-ui" data-nosnippet>
|
||||
|
||||
@@ -1,79 +1,78 @@
|
||||
.page {
|
||||
position: relative;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
/* Notemark CSS Variables for Light/Dark Theme */
|
||||
:root {
|
||||
/* Light Mode (Default) */
|
||||
--bg: #F9FAFB;
|
||||
--surface: #FFFFFF;
|
||||
--border: #E5E7EB;
|
||||
--primary-text: #111827;
|
||||
--secondary-text: #6B7280;
|
||||
--accent: #4F46E5;
|
||||
--accent-hover: #4338CA;
|
||||
--success: #10B981;
|
||||
--error: #F43F5E;
|
||||
}
|
||||
|
||||
main {
|
||||
flex: 1;
|
||||
[data-theme="dark"] {
|
||||
/* Dark Mode */
|
||||
--bg: #0B0F19;
|
||||
--surface: #111827;
|
||||
--border: #1F2937;
|
||||
--primary-text: #F9FAFB;
|
||||
--secondary-text: #9CA3AF;
|
||||
--accent: #22D3EE;
|
||||
--accent-hover: #06B6D4;
|
||||
--success: #4ADE80;
|
||||
--error: #FB7185;
|
||||
}
|
||||
|
||||
.sidebar {
|
||||
background-image: linear-gradient(180deg, rgb(5, 39, 103) 0%, #3a0647 70%);
|
||||
}
|
||||
|
||||
.top-row {
|
||||
background-color: #f7f7f7;
|
||||
border-bottom: 1px solid #d6d5d5;
|
||||
justify-content: flex-end;
|
||||
height: 3.5rem;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.top-row ::deep a, .top-row ::deep .btn-link {
|
||||
white-space: nowrap;
|
||||
margin-left: 1.5rem;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.top-row ::deep a:hover, .top-row ::deep .btn-link:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.top-row ::deep a:first-child {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
@media (max-width: 640.98px) {
|
||||
.top-row {
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.top-row ::deep a, .top-row ::deep .btn-link {
|
||||
margin-left: 0;
|
||||
/* Auto-detect system preference */
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--bg: #0B0F19;
|
||||
--surface: #111827;
|
||||
--border: #1F2937;
|
||||
--primary-text: #F9FAFB;
|
||||
--secondary-text: #9CA3AF;
|
||||
--accent: #22D3EE;
|
||||
--accent-hover: #06B6D4;
|
||||
--success: #4ADE80;
|
||||
--error: #FB7185;
|
||||
}
|
||||
}
|
||||
|
||||
@media (min-width: 641px) {
|
||||
.page {
|
||||
flex-direction: row;
|
||||
/* Override system dark mode when explicitly set to light */
|
||||
[data-theme="light"] {
|
||||
--bg: #F9FAFB;
|
||||
--surface: #FFFFFF;
|
||||
--border: #E5E7EB;
|
||||
--primary-text: #111827;
|
||||
--secondary-text: #6B7280;
|
||||
--accent: #4F46E5;
|
||||
--accent-hover: #4338CA;
|
||||
--success: #10B981;
|
||||
--error: #F43F5E;
|
||||
}
|
||||
|
||||
.sidebar {
|
||||
width: 250px;
|
||||
/* Notemark App Layout */
|
||||
.notemark-app {
|
||||
height: 100vh;
|
||||
position: sticky;
|
||||
top: 0;
|
||||
width: 100vw;
|
||||
background-color: var(--bg);
|
||||
color: var(--primary-text);
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.top-row {
|
||||
position: sticky;
|
||||
top: 0;
|
||||
z-index: 1;
|
||||
/* Reset default margins and padding */
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.top-row.auth ::deep a:first-child {
|
||||
flex: 1;
|
||||
text-align: right;
|
||||
width: 0;
|
||||
}
|
||||
|
||||
.top-row, article {
|
||||
padding-left: 2rem !important;
|
||||
padding-right: 1.5rem !important;
|
||||
}
|
||||
body {
|
||||
background-color: var(--bg);
|
||||
color: var(--primary-text);
|
||||
}
|
||||
|
||||
#blazor-error-ui {
|
||||
|
||||
@@ -1,30 +1,2 @@
|
||||
<div class="top-row ps-3 navbar navbar-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="">Notemark</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<input type="checkbox" title="Navigation menu" class="navbar-toggler" />
|
||||
|
||||
<div class="nav-scrollable" onclick="document.querySelector('.navbar-toggler').click()">
|
||||
<nav class="nav flex-column">
|
||||
<div class="nav-item px-3">
|
||||
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
|
||||
<span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Home
|
||||
</NavLink>
|
||||
</div>
|
||||
|
||||
<div class="nav-item px-3">
|
||||
<NavLink class="nav-link" href="counter">
|
||||
<span class="bi bi-plus-square-fill-nav-menu" aria-hidden="true"></span> Counter
|
||||
</NavLink>
|
||||
</div>
|
||||
|
||||
<div class="nav-item px-3">
|
||||
<NavLink class="nav-link" href="weather">
|
||||
<span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Weather
|
||||
</NavLink>
|
||||
</div>
|
||||
</nav>
|
||||
</div>
|
||||
@* Notemark uses a minimalist layout with no navigation menu *@
|
||||
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
@page "/counter"
|
||||
@rendermode InteractiveServer
|
||||
|
||||
<PageTitle>Counter</PageTitle>
|
||||
|
||||
<h1>Counter</h1>
|
||||
|
||||
<p role="status">Current count: @currentCount</p>
|
||||
|
||||
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
|
||||
|
||||
@code {
|
||||
private int currentCount = 0;
|
||||
|
||||
private void IncrementCount()
|
||||
{
|
||||
currentCount++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
@page "/"
|
||||
@rendermode InteractiveServer
|
||||
@using Notemark.Services
|
||||
@inject ISessionService SessionService
|
||||
@inject IMarkdownService MarkdownService
|
||||
@inject IShareService ShareService
|
||||
@inject IJSRuntime JSRuntime
|
||||
@inject IHttpContextAccessor HttpContextAccessor
|
||||
|
||||
<PageTitle>Notemark</PageTitle>
|
||||
|
||||
<div class="editor-container">
|
||||
<!-- Top Bar -->
|
||||
<div class="top-bar">
|
||||
<div class="top-bar-left">
|
||||
<button class="btn-toggle @(showPreview ? "active" : "")"
|
||||
@onclick="TogglePreview">
|
||||
@(showPreview ? "Hide Preview" : "Show Preview")
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="top-bar-center">
|
||||
<span class="save-status @saveStatus.ToLower()">@saveStatus</span>
|
||||
</div>
|
||||
|
||||
<div class="top-bar-right">
|
||||
<button class="btn-share" @onclick="ShowShareModal">Share</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Editor Layout -->
|
||||
<div class="editor-layout @(showPreview ? "split-view" : "full-view")">
|
||||
<!-- Editor Pane -->
|
||||
<div class="editor-pane">
|
||||
<textarea class="markdown-editor"
|
||||
@bind="markdownContent"
|
||||
@oninput="OnContentChanged"
|
||||
@onkeydown="OnKeyDown"
|
||||
placeholder="Start typing your markdown..."
|
||||
spellcheck="true">
|
||||
</textarea>
|
||||
</div>
|
||||
|
||||
<!-- Preview Pane -->
|
||||
@if (showPreview)
|
||||
{
|
||||
<div class="preview-pane">
|
||||
<div class="preview-content">
|
||||
@((MarkupString)renderedHtml)
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<!-- Share Modal -->
|
||||
@if (showShareModal)
|
||||
{
|
||||
<div class="modal-overlay" @onclick="HideShareModal">
|
||||
<div class="modal-content" @onclick:stopPropagation="true">
|
||||
<div class="modal-header">
|
||||
<h3>Share Document</h3>
|
||||
<button class="btn-close" @onclick="HideShareModal">×</button>
|
||||
</div>
|
||||
|
||||
<div class="modal-body">
|
||||
@if (isGeneratingLinks)
|
||||
{
|
||||
<div class="loading">Generating share links...</div>
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(readonlyLink))
|
||||
{
|
||||
<div class="share-link-group">
|
||||
<label>Readonly Link (View Only)</label>
|
||||
<div class="link-input-group">
|
||||
<input type="text" readonly value="@readonlyLink" class="link-input" />
|
||||
<button @onclick="() => CopyToClipboard(readonlyLink)" class="btn-copy">Copy</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="share-link-group">
|
||||
<label>Editable Link (Can Edit)</label>
|
||||
<div class="link-input-group">
|
||||
<input type="text" readonly value="@editableLink" class="link-input" />
|
||||
<button @onclick="() => CopyToClipboard(editableLink)" class="btn-copy">Copy</button>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
@code {
|
||||
private string sessionId = "";
|
||||
private string markdownContent = "";
|
||||
private string renderedHtml = "";
|
||||
private bool showPreview = false;
|
||||
private string saveStatus = "Saved";
|
||||
|
||||
// Share modal state
|
||||
private bool showShareModal = false;
|
||||
private bool isGeneratingLinks = false;
|
||||
private string readonlyLink = "";
|
||||
private string editableLink = "";
|
||||
|
||||
// Auto-save timer
|
||||
private Timer? autoSaveTimer;
|
||||
private bool hasUnsavedChanges = false;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
// Get or create session
|
||||
var httpContext = HttpContextAccessor.HttpContext;
|
||||
if (httpContext != null)
|
||||
{
|
||||
sessionId = SessionService.GetOrCreateSessionId(httpContext);
|
||||
|
||||
// Load existing content
|
||||
markdownContent = await SessionService.LoadSessionContentAsync(sessionId);
|
||||
|
||||
// Render initial preview if needed
|
||||
if (showPreview)
|
||||
{
|
||||
renderedHtml = MarkdownService.RenderToHtml(markdownContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task OnContentChanged(ChangeEventArgs e)
|
||||
{
|
||||
markdownContent = e.Value?.ToString() ?? "";
|
||||
hasUnsavedChanges = true;
|
||||
saveStatus = "Saving...";
|
||||
|
||||
// Update preview if visible
|
||||
if (showPreview)
|
||||
{
|
||||
renderedHtml = MarkdownService.RenderToHtml(markdownContent);
|
||||
}
|
||||
|
||||
// Debounce auto-save
|
||||
autoSaveTimer?.Dispose();
|
||||
autoSaveTimer = new Timer(async _ => await AutoSave(), null, 1000, Timeout.Infinite);
|
||||
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
private async Task OnKeyDown(KeyboardEventArgs e)
|
||||
{
|
||||
// Ctrl+S or Cmd+S for manual save
|
||||
if ((e.CtrlKey || e.MetaKey) && e.Key == "s")
|
||||
{
|
||||
await SaveContent();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task AutoSave()
|
||||
{
|
||||
if (hasUnsavedChanges)
|
||||
{
|
||||
await SaveContent();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SaveContent()
|
||||
{
|
||||
if (string.IsNullOrEmpty(sessionId))
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
await SessionService.SaveSessionContentAsync(sessionId, markdownContent);
|
||||
hasUnsavedChanges = false;
|
||||
saveStatus = "Saved";
|
||||
}
|
||||
catch
|
||||
{
|
||||
saveStatus = "Error";
|
||||
}
|
||||
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
private void TogglePreview()
|
||||
{
|
||||
showPreview = !showPreview;
|
||||
|
||||
if (showPreview)
|
||||
{
|
||||
renderedHtml = MarkdownService.RenderToHtml(markdownContent);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ShowShareModal()
|
||||
{
|
||||
showShareModal = true;
|
||||
isGeneratingLinks = true;
|
||||
readonlyLink = "";
|
||||
editableLink = "";
|
||||
|
||||
await InvokeAsync(StateHasChanged);
|
||||
|
||||
try
|
||||
{
|
||||
// Generate share tokens
|
||||
var readonlyToken = await ShareService.CreateShareTokenAsync(sessionId, "readonly");
|
||||
var editableToken = await ShareService.CreateShareTokenAsync(sessionId, "editable");
|
||||
|
||||
// Build URLs
|
||||
var httpContext = HttpContextAccessor.HttpContext;
|
||||
if (httpContext != null)
|
||||
{
|
||||
var baseUrl = $"{httpContext.Request.Scheme}://{httpContext.Request.Host}";
|
||||
readonlyLink = $"{baseUrl}/s/{readonlyToken}";
|
||||
editableLink = $"{baseUrl}/e/{editableToken}";
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
readonlyLink = "Error generating link";
|
||||
editableLink = "Error generating link";
|
||||
}
|
||||
finally
|
||||
{
|
||||
isGeneratingLinks = false;
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
}
|
||||
|
||||
private void HideShareModal()
|
||||
{
|
||||
showShareModal = false;
|
||||
}
|
||||
|
||||
private async Task CopyToClipboard(string text)
|
||||
{
|
||||
await JSRuntime.InvokeVoidAsync("navigator.clipboard.writeText", text);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
autoSaveTimer?.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,345 @@
|
||||
/* Notemark Editor Styles */
|
||||
|
||||
.editor-container {
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
background-color: var(--bg);
|
||||
}
|
||||
|
||||
/* Top Bar */
|
||||
.top-bar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 0.75rem 1rem;
|
||||
background-color: var(--surface);
|
||||
border-bottom: 1px solid var(--border);
|
||||
height: 60px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.top-bar-left,
|
||||
.top-bar-center,
|
||||
.top-bar-right {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.top-bar-center {
|
||||
flex: 1;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
/* Buttons */
|
||||
.btn-toggle,
|
||||
.btn-share,
|
||||
.btn-copy,
|
||||
.btn-close {
|
||||
background: none;
|
||||
border: 1px solid var(--border);
|
||||
color: var(--primary-text);
|
||||
padding: 0.5rem 1rem;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
font-size: 0.875rem;
|
||||
font-family: inherit;
|
||||
transition: all 0.2s ease;
|
||||
}
|
||||
|
||||
.btn-toggle:hover,
|
||||
.btn-share:hover,
|
||||
.btn-copy:hover {
|
||||
background-color: var(--accent);
|
||||
color: white;
|
||||
border-color: var(--accent);
|
||||
}
|
||||
|
||||
.btn-toggle.active {
|
||||
background-color: var(--accent);
|
||||
color: white;
|
||||
border-color: var(--accent);
|
||||
}
|
||||
|
||||
.btn-close {
|
||||
padding: 0.25rem 0.5rem;
|
||||
font-size: 1.25rem;
|
||||
line-height: 1;
|
||||
background: none;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.btn-close:hover {
|
||||
background-color: var(--error);
|
||||
color: white;
|
||||
}
|
||||
|
||||
/* Save Status */
|
||||
.save-status {
|
||||
font-size: 0.875rem;
|
||||
font-weight: 500;
|
||||
padding: 0.25rem 0.75rem;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.save-status.saved {
|
||||
color: var(--success);
|
||||
}
|
||||
|
||||
.save-status.saving {
|
||||
color: var(--secondary-text);
|
||||
}
|
||||
|
||||
.save-status.error {
|
||||
color: var(--error);
|
||||
}
|
||||
|
||||
/* Editor Layout */
|
||||
.editor-layout {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
.editor-layout.full-view {
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.editor-layout.split-view {
|
||||
flex-direction: row;
|
||||
}
|
||||
|
||||
/* Editor Pane */
|
||||
.editor-pane {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.split-view .editor-pane {
|
||||
border-right: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.markdown-editor {
|
||||
flex: 1;
|
||||
width: 100%;
|
||||
border: none;
|
||||
outline: none;
|
||||
resize: none;
|
||||
font-family: 'JetBrains Mono', 'SF Mono', 'Monaco', 'Inconsolata', 'Fira Code', monospace;
|
||||
font-size: 14px;
|
||||
line-height: 1.6;
|
||||
padding: 1.5rem;
|
||||
background-color: var(--bg);
|
||||
color: var(--primary-text);
|
||||
}
|
||||
|
||||
.markdown-editor::placeholder {
|
||||
color: var(--secondary-text);
|
||||
}
|
||||
|
||||
.markdown-editor:focus {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
/* Preview Pane */
|
||||
.preview-pane {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
background-color: var(--surface);
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.preview-content {
|
||||
flex: 1;
|
||||
padding: 1.5rem;
|
||||
overflow-y: auto;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
/* Markdown Preview Styles */
|
||||
.preview-content h1,
|
||||
.preview-content h2,
|
||||
.preview-content h3,
|
||||
.preview-content h4,
|
||||
.preview-content h5,
|
||||
.preview-content h6 {
|
||||
color: var(--primary-text);
|
||||
margin-top: 2em;
|
||||
margin-bottom: 1em;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.preview-content h1 { font-size: 2em; }
|
||||
.preview-content h2 { font-size: 1.5em; }
|
||||
.preview-content h3 { font-size: 1.25em; }
|
||||
|
||||
.preview-content p {
|
||||
margin-bottom: 1em;
|
||||
color: var(--primary-text);
|
||||
}
|
||||
|
||||
.preview-content code {
|
||||
background-color: var(--bg);
|
||||
padding: 0.2em 0.4em;
|
||||
border-radius: 3px;
|
||||
font-family: 'JetBrains Mono', 'SF Mono', 'Monaco', monospace;
|
||||
font-size: 0.875em;
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
.preview-content pre {
|
||||
background-color: var(--bg);
|
||||
padding: 1em;
|
||||
border-radius: 6px;
|
||||
overflow-x: auto;
|
||||
margin-bottom: 1em;
|
||||
border: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.preview-content pre code {
|
||||
background: none;
|
||||
padding: 0;
|
||||
color: var(--primary-text);
|
||||
}
|
||||
|
||||
.preview-content blockquote {
|
||||
border-left: 4px solid var(--accent);
|
||||
padding-left: 1em;
|
||||
margin-left: 0;
|
||||
margin-bottom: 1em;
|
||||
color: var(--secondary-text);
|
||||
}
|
||||
|
||||
.preview-content ul,
|
||||
.preview-content ol {
|
||||
margin-bottom: 1em;
|
||||
padding-left: 2em;
|
||||
}
|
||||
|
||||
.preview-content table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
.preview-content th,
|
||||
.preview-content td {
|
||||
border: 1px solid var(--border);
|
||||
padding: 0.5em 1em;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.preview-content th {
|
||||
background-color: var(--bg);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
/* Modal Styles */
|
||||
.modal-overlay {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
background-color: rgba(0, 0, 0, 0.5);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 1000;
|
||||
}
|
||||
|
||||
.modal-content {
|
||||
background-color: var(--surface);
|
||||
border-radius: 8px;
|
||||
width: 90%;
|
||||
max-width: 500px;
|
||||
max-height: 80vh;
|
||||
overflow: hidden;
|
||||
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
|
||||
}
|
||||
|
||||
.modal-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 1rem 1.5rem;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.modal-header h3 {
|
||||
margin: 0;
|
||||
font-size: 1.25rem;
|
||||
color: var(--primary-text);
|
||||
}
|
||||
|
||||
.modal-body {
|
||||
padding: 1.5rem;
|
||||
}
|
||||
|
||||
.share-link-group {
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.share-link-group:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.share-link-group label {
|
||||
display: block;
|
||||
margin-bottom: 0.5rem;
|
||||
font-weight: 500;
|
||||
color: var(--primary-text);
|
||||
}
|
||||
|
||||
.link-input-group {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.link-input {
|
||||
flex: 1;
|
||||
padding: 0.75rem;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 6px;
|
||||
background-color: var(--bg);
|
||||
color: var(--primary-text);
|
||||
font-family: monospace;
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.loading {
|
||||
text-align: center;
|
||||
color: var(--secondary-text);
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
/* Mobile Responsive */
|
||||
@media (max-width: 768px) {
|
||||
.editor-layout.split-view {
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.split-view .editor-pane {
|
||||
border-right: none;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.top-bar {
|
||||
padding: 0.5rem;
|
||||
}
|
||||
|
||||
.btn-toggle,
|
||||
.btn-share {
|
||||
font-size: 0.75rem;
|
||||
padding: 0.375rem 0.75rem;
|
||||
}
|
||||
|
||||
.markdown-editor,
|
||||
.preview-content {
|
||||
padding: 1rem;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
@page "/"
|
||||
|
||||
<PageTitle>Home</PageTitle>
|
||||
|
||||
<h1>Hello, world!</h1>
|
||||
|
||||
Welcome to your new app.
|
||||
@@ -1,64 +0,0 @@
|
||||
@page "/weather"
|
||||
@attribute [StreamRendering]
|
||||
|
||||
<PageTitle>Weather</PageTitle>
|
||||
|
||||
<h1>Weather</h1>
|
||||
|
||||
<p>This component demonstrates showing data.</p>
|
||||
|
||||
@if (forecasts == null)
|
||||
{
|
||||
<p><em>Loading...</em></p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th aria-label="Temperature in Celsius">Temp. (C)</th>
|
||||
<th aria-label="Temperature in Fahrenheit">Temp. (F)</th>
|
||||
<th>Summary</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var forecast in forecasts)
|
||||
{
|
||||
<tr>
|
||||
<td>@forecast.Date.ToShortDateString()</td>
|
||||
<td>@forecast.TemperatureC</td>
|
||||
<td>@forecast.TemperatureF</td>
|
||||
<td>@forecast.Summary</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
}
|
||||
|
||||
@code {
|
||||
private WeatherForecast[]? forecasts;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
// Simulate asynchronous loading to demonstrate streaming rendering
|
||||
await Task.Delay(500);
|
||||
|
||||
var startDate = DateOnly.FromDateTime(DateTime.Now);
|
||||
var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
|
||||
forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
{
|
||||
Date = startDate.AddDays(index),
|
||||
TemperatureC = Random.Shared.Next(-20, 55),
|
||||
Summary = summaries[Random.Shared.Next(summaries.Length)]
|
||||
}).ToArray();
|
||||
}
|
||||
|
||||
private class WeatherForecast
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
public int TemperatureC { get; set; }
|
||||
public string? Summary { get; set; }
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
}
|
||||
}
|
||||
@@ -7,4 +7,9 @@
|
||||
<BlazorDisableThrowNavigationException>true</BlazorDisableThrowNavigationException>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Markdig" Version="0.37.0" />
|
||||
<PackageReference Include="HtmlSanitizer" Version="8.0.865" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Notemark.Components;
|
||||
using Notemark.Services;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@@ -6,6 +7,14 @@ var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddRazorComponents()
|
||||
.AddInteractiveServerComponents();
|
||||
|
||||
// Add HTTP context accessor for session management
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
|
||||
// Register Notemark services
|
||||
builder.Services.AddSingleton<IMarkdownService, MarkdownService>();
|
||||
builder.Services.AddScoped<ISessionService, SessionService>();
|
||||
builder.Services.AddScoped<IShareService, ShareService>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
@@ -18,6 +27,13 @@ if (!app.Environment.IsDevelopment())
|
||||
app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
// Enable cookies for session management
|
||||
app.UseCookiePolicy(new CookiePolicyOptions
|
||||
{
|
||||
MinimumSameSitePolicy = SameSiteMode.Lax,
|
||||
Secure = CookieSecurePolicy.SameAsRequest
|
||||
});
|
||||
|
||||
app.UseAntiforgery();
|
||||
|
||||
app.MapStaticAssets();
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Notemark.Services;
|
||||
|
||||
public interface IMarkdownService
|
||||
{
|
||||
string RenderToHtml(string markdown);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Notemark.Services;
|
||||
|
||||
public interface ISessionService
|
||||
{
|
||||
string GetOrCreateSessionId(HttpContext httpContext);
|
||||
Task<string> LoadSessionContentAsync(string sessionId);
|
||||
Task SaveSessionContentAsync(string sessionId, string content);
|
||||
Task<bool> SessionExistsAsync(string sessionId);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace Notemark.Services;
|
||||
|
||||
public record ShareToken(
|
||||
string SessionId,
|
||||
string Mode, // "readonly" or "editable"
|
||||
DateTime CreatedAt,
|
||||
DateTime? ExpiresAt = null,
|
||||
DateTime? RevokedAt = null
|
||||
);
|
||||
|
||||
public interface IShareService
|
||||
{
|
||||
Task<string> CreateShareTokenAsync(string sessionId, string mode);
|
||||
Task<ShareToken?> GetShareTokenAsync(string token);
|
||||
Task RevokeShareTokenAsync(string token);
|
||||
Task<bool> IsValidTokenAsync(string token);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Markdig;
|
||||
using Ganss.Xss;
|
||||
|
||||
namespace Notemark.Services;
|
||||
|
||||
public class MarkdownService : IMarkdownService
|
||||
{
|
||||
private readonly MarkdownPipeline _pipeline;
|
||||
private readonly HtmlSanitizer _sanitizer;
|
||||
|
||||
public MarkdownService()
|
||||
{
|
||||
// Configure Markdig pipeline with GitHub-style features
|
||||
_pipeline = new MarkdownPipelineBuilder()
|
||||
.UseAdvancedExtensions() // Tables, footnotes, task lists, etc.
|
||||
.UseSoftlineBreakAsHardlineBreak() // GitHub-style line breaks
|
||||
.Build();
|
||||
|
||||
// Configure HTML sanitizer for security
|
||||
_sanitizer = new HtmlSanitizer();
|
||||
_sanitizer.AllowedTags.Add("details");
|
||||
_sanitizer.AllowedTags.Add("summary");
|
||||
_sanitizer.AllowedAttributes.Add("class");
|
||||
_sanitizer.AllowedAttributes.Add("id");
|
||||
}
|
||||
|
||||
public string RenderToHtml(string markdown)
|
||||
{
|
||||
if (string.IsNullOrEmpty(markdown))
|
||||
return string.Empty;
|
||||
|
||||
// Convert markdown to HTML
|
||||
var html = Markdown.ToHtml(markdown, _pipeline);
|
||||
|
||||
// Sanitize HTML to prevent XSS attacks
|
||||
return _sanitizer.Sanitize(html);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
namespace Notemark.Services;
|
||||
|
||||
public class SessionService : ISessionService
|
||||
{
|
||||
private readonly string _dataPath;
|
||||
private const string SessionCookieName = "mk_session";
|
||||
|
||||
public SessionService(IWebHostEnvironment environment)
|
||||
{
|
||||
_dataPath = Path.Combine(environment.ContentRootPath, "data", "sessions");
|
||||
Directory.CreateDirectory(_dataPath);
|
||||
}
|
||||
|
||||
public string GetOrCreateSessionId(HttpContext httpContext)
|
||||
{
|
||||
// Try to get existing session from cookie
|
||||
if (httpContext.Request.Cookies.TryGetValue(SessionCookieName, out var existingSessionId)
|
||||
&& Guid.TryParse(existingSessionId, out _))
|
||||
{
|
||||
return existingSessionId;
|
||||
}
|
||||
|
||||
// Create new session
|
||||
var newSessionId = Guid.NewGuid().ToString();
|
||||
|
||||
// Set cookie with 30-day expiration
|
||||
var cookieOptions = new CookieOptions
|
||||
{
|
||||
Expires = DateTimeOffset.UtcNow.AddDays(30),
|
||||
HttpOnly = true,
|
||||
Secure = true,
|
||||
SameSite = SameSiteMode.Lax
|
||||
};
|
||||
|
||||
httpContext.Response.Cookies.Append(SessionCookieName, newSessionId, cookieOptions);
|
||||
|
||||
return newSessionId;
|
||||
}
|
||||
|
||||
public async Task<string> LoadSessionContentAsync(string sessionId)
|
||||
{
|
||||
var filePath = GetSessionFilePath(sessionId);
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
return string.Empty;
|
||||
|
||||
return await File.ReadAllTextAsync(filePath);
|
||||
}
|
||||
|
||||
public async Task SaveSessionContentAsync(string sessionId, string content)
|
||||
{
|
||||
var filePath = GetSessionFilePath(sessionId);
|
||||
await File.WriteAllTextAsync(filePath, content ?? string.Empty);
|
||||
}
|
||||
|
||||
public async Task<bool> SessionExistsAsync(string sessionId)
|
||||
{
|
||||
var filePath = GetSessionFilePath(sessionId);
|
||||
return File.Exists(filePath);
|
||||
}
|
||||
|
||||
private string GetSessionFilePath(string sessionId)
|
||||
{
|
||||
if (!Guid.TryParse(sessionId, out _))
|
||||
throw new ArgumentException("Invalid session ID format", nameof(sessionId));
|
||||
|
||||
return Path.Combine(_dataPath, $"{sessionId}.md");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Notemark.Services;
|
||||
|
||||
public class ShareService : IShareService
|
||||
{
|
||||
private readonly string _dataPath;
|
||||
private readonly JsonSerializerOptions _jsonOptions;
|
||||
|
||||
public ShareService(IWebHostEnvironment environment)
|
||||
{
|
||||
_dataPath = Path.Combine(environment.ContentRootPath, "data", "shares");
|
||||
Directory.CreateDirectory(_dataPath);
|
||||
|
||||
_jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = true
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<string> CreateShareTokenAsync(string sessionId, string mode)
|
||||
{
|
||||
if (!Guid.TryParse(sessionId, out _))
|
||||
throw new ArgumentException("Invalid session ID format", nameof(sessionId));
|
||||
|
||||
if (mode != "readonly" && mode != "editable")
|
||||
throw new ArgumentException("Mode must be 'readonly' or 'editable'", nameof(mode));
|
||||
|
||||
var token = GenerateToken();
|
||||
var shareToken = new ShareToken(
|
||||
SessionId: sessionId,
|
||||
Mode: mode,
|
||||
CreatedAt: DateTime.UtcNow
|
||||
);
|
||||
|
||||
var filePath = GetTokenFilePath(token);
|
||||
var json = JsonSerializer.Serialize(shareToken, _jsonOptions);
|
||||
await File.WriteAllTextAsync(filePath, json);
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
public async Task<ShareToken?> GetShareTokenAsync(string token)
|
||||
{
|
||||
var filePath = GetTokenFilePath(token);
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
return null;
|
||||
|
||||
try
|
||||
{
|
||||
var json = await File.ReadAllTextAsync(filePath);
|
||||
return JsonSerializer.Deserialize<ShareToken>(json, _jsonOptions);
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RevokeShareTokenAsync(string token)
|
||||
{
|
||||
var shareToken = await GetShareTokenAsync(token);
|
||||
if (shareToken == null)
|
||||
return;
|
||||
|
||||
var revokedToken = shareToken with { RevokedAt = DateTime.UtcNow };
|
||||
var filePath = GetTokenFilePath(token);
|
||||
var json = JsonSerializer.Serialize(revokedToken, _jsonOptions);
|
||||
await File.WriteAllTextAsync(filePath, json);
|
||||
}
|
||||
|
||||
public async Task<bool> IsValidTokenAsync(string token)
|
||||
{
|
||||
var shareToken = await GetShareTokenAsync(token);
|
||||
|
||||
if (shareToken == null)
|
||||
return false;
|
||||
|
||||
// Check if revoked
|
||||
if (shareToken.RevokedAt.HasValue)
|
||||
return false;
|
||||
|
||||
// Check if expired
|
||||
if (shareToken.ExpiresAt.HasValue && shareToken.ExpiresAt.Value < DateTime.UtcNow)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private string GenerateToken()
|
||||
{
|
||||
// Generate a URL-safe random token
|
||||
return Convert.ToBase64String(Guid.NewGuid().ToByteArray())
|
||||
.Replace('+', '-')
|
||||
.Replace('/', '_')
|
||||
.Replace("=", "");
|
||||
}
|
||||
|
||||
private string GetTokenFilePath(string token)
|
||||
{
|
||||
// Sanitize token for filename
|
||||
var sanitizedToken = string.Concat(token.Where(c => char.IsLetterOrDigit(c) || c == '-' || c == '_'));
|
||||
return Path.Combine(_dataPath, $"{sanitizedToken}.json");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
# Notemark — .NET 10 Blazor Server Specification (Markdown Editor + Preview + Share)
|
||||
|
||||
## 0) Goal
|
||||
Notemark is a minimalist Markdown editor web app built with **.NET 10 Blazor Server**.
|
||||
|
||||
- First visit generates a **GUID sessionId**
|
||||
- Markdown is saved as a **file on disk** (no database)
|
||||
- Fullscreen editor, optional preview split view
|
||||
- Share links:
|
||||
- **Readonly** → preview only
|
||||
- **Editable** → preview only + Edit button
|
||||
|
||||
---
|
||||
|
||||
## 1) Tech Stack
|
||||
- .NET 10
|
||||
- Blazor Server
|
||||
- Markdown rendering: Markdig
|
||||
- HTML sanitization: Ganss.XSS (HtmlSanitizer)
|
||||
- Persistence: File system only
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"sessionId": "b5acffbe-706e-405e-b006-3415508a59a7",
|
||||
"mode": "editable",
|
||||
"createdAt": "2026-01-30T15:19:41.7110595Z",
|
||||
"expiresAt": null,
|
||||
"revokedAt": null
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"sessionId": "b5acffbe-706e-405e-b006-3415508a59a7",
|
||||
"mode": "readonly",
|
||||
"createdAt": "2026-01-30T15:19:41.7093218Z",
|
||||
"expiresAt": null,
|
||||
"revokedAt": null
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"sessionId": "b5acffbe-706e-405e-b006-3415508a59a7",
|
||||
"mode": "readonly",
|
||||
"createdAt": "2026-01-30T15:19:24.7974267Z",
|
||||
"expiresAt": null,
|
||||
"revokedAt": null
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"sessionId": "b5acffbe-706e-405e-b006-3415508a59a7",
|
||||
"mode": "editable",
|
||||
"createdAt": "2026-01-30T15:19:24.8076694Z",
|
||||
"expiresAt": null,
|
||||
"revokedAt": null
|
||||
}
|
||||
Reference in New Issue
Block a user