Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ side-by-side work:
<img src="docs/images/search-light.png" alt="MarkPad file search sidebar with Open files selected: a search for open file finds three matching lines across two unsaved drafts beside the editor and preview">
</picture>

- **Document outline.** Open the outline sidebar from the toolbar or with `Ctrl/⌘+Shift+O` to browse every heading of the current Markdown file as an indented table of contents that updates as you type. Clicking a heading jumps straight to it — the editor puts it at the top of the view, and the preview follows in split and preview modes.
- **In-document link navigation.** Headings get anchor ids, so clicking an in-page link in the preview — like a table of contents `[Section](#section)` — smooth-scrolls to that heading within the preview pane.
- **Links open where they belong.** Web and mail links in the preview open in your default browser or mail app, and links to other Markdown, JSON, or YAML files (`[Guide](docs/guide.md)`) open them in markpad, resolved relative to the current file. The markpad window itself only ever shows markpad: links, forms, and pop-ups can never turn it into a browser.
- **Diagrams from fenced code.** A ` ```mermaid ` block renders as a diagram in the preview — flowcharts, sequence, class, state, ER, gantt, pie, mindmap, timeline, git graphs and the rest of [Mermaid](https://mermaid.js.org/)'s catalogue — and ` ```dot ` (or `graphviz`, `gv`) renders [Graphviz](https://graphviz.org/) DOT source. Both engines run entirely on your machine, follow the app's light/dark theme, and load only when a document actually has a diagram in it. Source that does not parse shows the engine's message inline with the block, so a half-typed diagram never blanks the preview.
Expand Down
52 changes: 51 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import ErrorBanner from "./components/ErrorBanner";
import ExternalChangeBanner from "./components/ExternalChangeBanner";
import RecentsPanel, { type RecentEntry } from "./components/RecentsPanel";
import WorkspaceSearchPanel from "./components/WorkspaceSearchPanel";
import OutlinePanel from "./components/OutlinePanel";
import { markdownHeadings } from "./lib/markdown";
import ConfirmDialog from "./components/ConfirmDialog";
import type { EditorHandle } from "./components/Editor";
import type { PreviewHandle } from "./components/Preview";
Expand Down Expand Up @@ -111,7 +113,7 @@ type ItemSnapshot = {
previewScrollTop: number;
};

type SidebarView = "recents" | "workspaceSearch";
type SidebarView = "recents" | "workspaceSearch" | "outline";

type PendingLocation = {
itemId: ItemId;
Expand Down Expand Up @@ -276,6 +278,13 @@ function App() {
const searchOpen = activeId !== null && searchItemId === activeId;
const workspaceSearchOpen =
!sidebarCollapsed && sidebarView === "workspaceSearch";
const outlineOpen = !sidebarCollapsed && sidebarView === "outline";
const activeIsMarkdown = activeItem !== null && activeLanguage === "markdown";
// Parsed only while the outline is on screen; it follows every keystroke.
const outlineHeadings = useMemo(
() => (outlineOpen && activeIsMarkdown ? markdownHeadings(activeText) : []),
[outlineOpen, activeIsMarkdown, activeText],
);

const recentEntries = useMemo<RecentEntry[]>(
() =>
Expand Down Expand Up @@ -1115,6 +1124,29 @@ function App() {
setSidebarView("recents");
}

function handleToggleOutline() {
if (outlineOpen) {
setSidebarView("recents");
return;
}
setSidebarView("outline");
if (sidebarCollapsed) {
setSidebarCollapsed(false);
persistSidebarCollapsed(false);
}
}

// Jump to a heading picked in the outline, placing it at the top of the view
// so its section reads downward. In split view the preview follows the
// editor through scroll sync.
function handleOutlineSelect(line: number) {
if (viewMode === "preview") {
previewRef.current?.scrollToSyncLine(line);
} else {
editorRef.current?.revealLine(line, "start");
}
}

async function handleChooseWorkspaceRoot() {
const result = await openWorkspaceFolder();
if (result.kind === "cancelled") return;
Expand Down Expand Up @@ -1283,6 +1315,7 @@ function App() {
const handleToggleSidebarRef = useRef(handleToggleSidebar);
const handleOpenSearchRef = useRef(handleOpenSearch);
const handleOpenWorkspaceSearchRef = useRef(handleOpenWorkspaceSearch);
const handleToggleOutlineRef = useRef(handleToggleOutline);
const handleCloseSearchRef = useRef(handleCloseSearch);
const checkAgainstDiskRef = useRef(checkOpenFilesAgainstDisk);

Expand All @@ -1293,6 +1326,7 @@ function App() {
handleToggleSidebarRef.current = handleToggleSidebar;
handleOpenSearchRef.current = handleOpenSearch;
handleOpenWorkspaceSearchRef.current = handleOpenWorkspaceSearch;
handleToggleOutlineRef.current = handleToggleOutline;
handleCloseSearchRef.current = handleCloseSearch;
checkAgainstDiskRef.current = checkOpenFilesAgainstDisk;
});
Expand Down Expand Up @@ -1331,6 +1365,12 @@ function App() {
return;
}
const mod = e.ctrlKey || e.metaKey;
if (mod && e.shiftKey && !e.altKey && e.key.toLowerCase() === "o") {
e.preventDefault();
e.stopPropagation();
handleToggleOutlineRef.current();
return;
}
if (!mod || e.shiftKey || e.altKey) return;
const key = e.key.toLowerCase();
if (key === "s") {
Expand Down Expand Up @@ -1388,6 +1428,14 @@ function App() {
onRemove={handleRemove}
onRemoveMany={handleRemoveMany}
/>
) : sidebarView === "outline" ? (
<OutlinePanel
fileName={activeItem?.name ?? null}
isMarkdown={activeIsMarkdown}
headings={outlineHeadings}
onSelect={handleOutlineSelect}
onClose={() => setSidebarView("recents")}
/>
) : (
<WorkspaceSearchPanel
scope={workspaceSearchScope}
Expand Down Expand Up @@ -1430,13 +1478,15 @@ function App() {
sidebarCollapsed={sidebarCollapsed}
searchEnabled={activeItem !== null}
workspaceSearchOpen={workspaceSearchOpen}
outlineOpen={outlineOpen}
modKey={modKey}
onToggleSidebar={handleToggleSidebar}
onNewFile={handleNewFile}
onOpenFile={handleOpenFile}
onSave={handleSave}
onFind={handleOpenSearch}
onSearchFiles={handleOpenWorkspaceSearch}
onToggleOutline={handleToggleOutline}
onToggleAutoSave={handleToggleAutoSave}
onSetViewMode={handleSetViewMode}
onToggleTheme={handleToggleTheme}
Expand Down
9 changes: 6 additions & 3 deletions src/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ export type EditorHandle = {
clearSearch(): void;
getSelectedText(): string;
focus(): void;
revealLine(lineNumber: number): void;
/** Put the caret at the start of a line and scroll it into view: centred
by default, or at the top for `"start"` (a heading jumped to from the
outline, so its section reads downward from there). */
revealLine(lineNumber: number, align?: "center" | "start"): void;
getScrollTop(): number;
/** Source line shown at the top of the viewport, or null before mount. */
getSyncLine(): number | null;
Expand Down Expand Up @@ -430,15 +433,15 @@ const Editor = forwardRef<EditorHandle, EditorProps>(function Editor(
return view.state.sliceDoc(from, to);
},
focus: () => viewRef.current?.focus(),
revealLine: (lineNumber) => {
revealLine: (lineNumber, align = "center") => {
const view = viewRef.current;
if (!view) return;
const line = view.state.doc.line(
Math.min(view.state.doc.lines, Math.max(1, lineNumber)),
);
view.dispatch({
selection: { anchor: line.from },
effects: EditorView.scrollIntoView(line.from, { y: "center" }),
effects: EditorView.scrollIntoView(line.from, { y: align }),
});
view.focus();
},
Expand Down
153 changes: 153 additions & 0 deletions src/components/OutlinePanel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import type { Heading } from "../lib/markdown";

type OutlinePanelProps = {
/** Name of the active document, or null when nothing is open. */
fileName: string | null;
/** Only Markdown has headings; JSON and YAML get an explanation instead. */
isMarkdown: boolean;
headings: Heading[];
onSelect: (line: number) => void;
onClose: () => void;
};

const iconButton =
"inline-flex size-7 items-center justify-center rounded-md text-[color:var(--muted)] transition-colors hover:bg-[color:var(--hover)] hover:text-[color:var(--text)] focus:outline-none focus-visible:ring-2 focus-visible:ring-[color:var(--accent)]";

// Each level below the document's shallowest heading indents by this much, so a
// document that starts at `##` is not pushed in by an empty first column.
const INDENT_REM = 0.75;

function OutlineIcon({ size = 18 }: { size?: number }) {
return (
<svg
aria-hidden="true"
viewBox="0 0 24 24"
width={size}
height={size}
fill="none"
stroke="currentColor"
strokeWidth="1.7"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="M4 6h.01M8 6h12M7 12h.01M11 12h9M7 18h.01M11 18h9" />
</svg>
);
}

function CloseIcon() {
return (
<svg
aria-hidden="true"
viewBox="0 0 24 24"
width="16"
height="16"
fill="none"
stroke="currentColor"
strokeWidth="1.8"
strokeLinecap="round"
>
<path d="m6 6 12 12M18 6 6 18" />
</svg>
);
}

function EmptyState({ title, detail }: { title: string; detail: string }) {
return (
<div className="px-5 py-10 text-center">
<div className="mx-auto mb-3 flex size-10 items-center justify-center rounded-full border border-[color:var(--border)] text-[color:var(--muted)]">
<OutlineIcon size={20} />
</div>
<p className="text-sm font-medium text-[color:var(--text)]">{title}</p>
<p className="mt-1 text-xs leading-relaxed text-[color:var(--muted)]">
{detail}
</p>
</div>
);
}

export default function OutlinePanel({
fileName,
isMarkdown,
headings,
onSelect,
onClose,
}: OutlinePanelProps) {
const topLevel = Math.min(...headings.map((heading) => heading.level));

return (
<section
className="flex h-full min-h-0 flex-col bg-[color:var(--panel)]"
aria-label="Outline"
>
<header className="flex items-center gap-2 border-b border-[color:var(--border)] px-3 py-2.5">
<div className="flex size-7 items-center justify-center rounded-md bg-[color:var(--accent-soft)] text-[color:var(--accent)]">
<OutlineIcon />
</div>
<div className="min-w-0 flex-1">
<h2 className="text-sm font-semibold tracking-tight text-[color:var(--text)]">
Outline
</h2>
<p
className="truncate text-[11px] leading-tight text-[color:var(--muted)]"
title={fileName ?? undefined}
>
{fileName ?? "No document"}
</p>
</div>
<button
type="button"
className={iconButton}
aria-label="Close outline"
title="Back to recent files"
onClick={onClose}
>
<CloseIcon />
</button>
</header>

<div className="min-h-0 flex-1 overflow-y-auto">
{fileName === null ? (
<EmptyState
title="No document open"
detail="Open or create a Markdown file to browse its headings here."
/>
) : !isMarkdown ? (
<EmptyState
title="No outline for this file"
detail="The outline lists the headings of Markdown documents."
/>
) : headings.length === 0 ? (
<EmptyState
title="No headings yet"
detail="Start a line with # to add a heading, and it will show up here."
/>
) : (
<nav aria-label="Document headings" className="py-1.5">
<ul>
{headings.map((heading) => (
<li key={`${heading.line}:${heading.text}`}>
<button
type="button"
onClick={() => onSelect(heading.line)}
title={`${heading.text} (line ${heading.line})`}
style={{
paddingLeft: `${0.75 + (heading.level - topLevel) * INDENT_REM}rem`,
}}
className={`block w-full truncate border-l-2 border-transparent py-1.5 pr-3 text-left text-xs transition-colors hover:border-[color:var(--accent)] hover:bg-[color:var(--hover)] focus:outline-none focus-visible:border-[color:var(--accent)] focus-visible:bg-[color:var(--accent-soft)] ${
heading.level === topLevel
? "font-semibold text-[color:var(--text)]"
: "text-[color:var(--muted)] hover:text-[color:var(--text)]"
}`}
>
{heading.text}
</button>
</li>
))}
</ul>
</nav>
)}
</div>
</section>
);
}
37 changes: 37 additions & 0 deletions src/components/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ type ToolbarProps = {
sidebarCollapsed: boolean;
searchEnabled: boolean;
workspaceSearchOpen: boolean;
outlineOpen: boolean;
modKey: string;
onToggleSidebar: () => void;
onNewFile: () => void;
onOpenFile: () => void;
onSave: () => void;
onFind: () => void;
onSearchFiles: () => void;
onToggleOutline: () => void;
onToggleAutoSave: (next: boolean) => void;
onSetViewMode: (mode: ViewMode) => void;
onToggleTheme: () => void;
Expand Down Expand Up @@ -197,6 +199,24 @@ function SearchFilesIcon() {
);
}

function OutlineIcon() {
return (
<svg
aria-hidden="true"
viewBox="0 0 24 24"
width="16"
height="16"
fill="none"
stroke="currentColor"
strokeWidth="1.75"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="M4 6h.01M8 6h12M7 12h.01M11 12h9M7 18h.01M11 18h9" />
</svg>
);
}

const segments: ReadonlyArray<{ mode: ViewMode; label: string }> = [
{ mode: "editor", label: "Editor" },
{ mode: "split", label: "Split" },
Expand All @@ -218,13 +238,15 @@ export default function Toolbar({
sidebarCollapsed,
searchEnabled,
workspaceSearchOpen,
outlineOpen,
modKey,
onToggleSidebar,
onNewFile,
onOpenFile,
onSave,
onFind,
onSearchFiles,
onToggleOutline,
onToggleAutoSave,
onSetViewMode,
onToggleTheme,
Expand Down Expand Up @@ -262,6 +284,21 @@ export default function Toolbar({
>
<SearchFilesIcon />
</button>
<button
type="button"
className={`${iconButton}${
outlineOpen
? " bg-[color:var(--accent-soft)] text-[color:var(--accent)]"
: ""
}`}
aria-label={`Outline (${modKey}+Shift+O)`}
aria-keyshortcuts="Control+Shift+O Meta+Shift+O"
aria-pressed={outlineOpen}
title={`Outline (${modKey}+Shift+O)`}
onClick={onToggleOutline}
>
<OutlineIcon />
</button>
<button
type="button"
className={iconButton}
Expand Down
Loading
Loading