Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 36 ยท Projects and Practice

Project: A Markdown Notes App

Build a complete Electron notes app plan: project layout, secure window setup, validated IPC, a notes store with atomic saves, live Markdown preview and tests, with runnable core logic.

Time to combine the chapters into one real application. Notes Studio is a small markdown notes app: a list of notes on the left, an editor in the middle, a live preview on the right, everything saved as files in the user's data folder. It touches almost every topic in this track: the main and renderer split, a narrow preload bridge, validated IPC, files and atomic saves, settings, native menus and packaging.

Electron itself cannot run in our editor, so the Electron wiring is shown as read-only code you can copy into a project made with Forge. The parts that carry the logic, the store, the Markdown renderer and the IPC contract, are plain Node modules, and they run right here. That is also exactly how you would test them.

What we are building

flowchart LR
  subgraph Renderer["Renderer (sandboxed page)"]
    UI["List, editor, preview"]
  end
  subgraph Preload
    API["window.notes API"]
  end
  subgraph Main["Main process"]
    H["Validated IPC handlers"]
    S["NoteStore (files in userData/notes)"]
  end
  UI --> API --> H --> S

The page only ever talks to four verbs: list, read, save, remove.

Project layout

TEXT
notes-studio/
  package.json
  forge.config.js
  src/
    main.js            window, menu, IPC wiring (Electron)
    preload.js         contextBridge API (Electron)
    note-store.js      plain module: file persistence
    markdown.js        plain module: Markdown to safe HTML
    validate.js        plain module: IPC payload checks
    renderer/
      index.html       with a strict CSP
      app.js
  test/
    note-store.test.js
    markdown.test.js

Everything under note-store.js, markdown.js and validate.js has no Electron import, so node --test covers it (Testing Electron Apps).

Step 1: the main process

JavaScript
// src/main.js
import { app, BrowserWindow, ipcMain, Menu } from 'electron';
import { join } from 'node:path';
import { NoteStore } from './note-store.js';
import { validateNote, validateId } from './validate.js';

let store;

function createWindow() {
  const win = new BrowserWindow({
    width: 1100, height: 720, show: false, backgroundColor: '#ffffff',
    webPreferences: {
      preload: join(import.meta.dirname, 'preload.js'),
      contextIsolation: true, sandbox: true, nodeIntegration: false,
    },
  });
  win.once('ready-to-show', () => win.show());
  win.webContents.setWindowOpenHandler(() => ({ action: 'deny' }));
  win.webContents.on('will-navigate', (e) => e.preventDefault());
  win.loadFile(join(import.meta.dirname, 'renderer/index.html'));
}

app.whenReady().then(async () => {
  store = new NoteStore(join(app.getPath('userData'), 'notes'));
  await store.init();

  ipcMain.handle('notes:list', () => store.list());
  ipcMain.handle('notes:read', (_e, id) => store.read(validateId(id)));
  ipcMain.handle('notes:save', (_e, note) => store.save(validateNote(note)));
  ipcMain.handle('notes:remove', (_e, id) => store.remove(validateId(id)));

  createWindow();
});

Each handler validates before it touches the store, following Validating IPC and Handling Secrets. The window flags are the safe defaults from the Security Checklist.

Step 2: the preload bridge

JavaScript
// src/preload.js
import { contextBridge, ipcRenderer } from 'electron';

contextBridge.exposeInMainWorld('notes', {
  list: () => ipcRenderer.invoke('notes:list'),
  read: (id) => ipcRenderer.invoke('notes:read', id),
  save: (note) => ipcRenderer.invoke('notes:save', note),
  remove: (id) => ipcRenderer.invoke('notes:remove', id),
});

Four named functions, four fixed channels. Nothing generic.

Step 3: the note store

The store keeps one JSON file per note, writes atomically, and refuses ids that could escape the folder. Because it is a plain module, it runs here exactly as it would in the app.

Node.js The NoteStore module

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice the corrupt broken.json is skipped, not fatal. One bad file must never stop a user reaching the other 500 notes.

Step 4: Markdown to safe HTML

The preview renders Markdown the user typed, which is untrusted content (it may even have been pasted from the web). Two rules: escape everything first, then add only the tags you generate. In a real app use a maintained library (marked or markdown-it with HTML disabled, plus DOMPurify); this miniature version shows the principle with a small subset.

Node.js A safe mini Markdown renderer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The second output is the important one: the <img> tag is escaped into harmless text, and the javascript: link does not match the https?:// pattern, so it stays plain text. Only your generated tags reach the page.

Step 5: the renderer

HTML
<!-- src/renderer/index.html -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:">
<aside id="list"></aside>
<textarea id="editor"></textarea>
<article id="preview"></article>
<script src="app.js" type="module"></script>
JavaScript
// src/renderer/app.js
import { renderMarkdown } from './markdown.js';

const editor = document.getElementById('editor');
const preview = document.getElementById('preview');
let current = null, timer;

editor.addEventListener('input', () => {
  preview.innerHTML = renderMarkdown(editor.value);
  clearTimeout(timer);
  timer = setTimeout(() => window.notes.save({ id: current.id, title: titleOf(editor.value), body: editor.value }), 400);
});

const titleOf = (text) => (text.match(/^#\s+(.*)$/m)?.[1] ?? 'Untitled').slice(0, 120);

The renderer debounces saves by 400 ms, exactly the pattern from App Data and Settings, and sets the preview with innerHTML only because the renderer already escaped the input. The CSP blocks inline scripts as a second line of defence.

Step 6: menu, shortcuts and polish

Add an application menu with New note (CmdOrCtrl+N), Delete and Quit, and wire the menu items to webContents.send events. Remember the last open note in settings, restore the window bounds, and show unsaved state in the title bar with win.setDocumentEdited(true) on macOS.

Step 7: ship it

Add icons and a bundle id in forge.config.js, flip the fuses, run npm run make on each platform and sign the result (Packaging with Electron Forge, Installers and Code Signing). Add an auto-update check (Auto-update Flow).

Extend it

  • Search across notes with a simple index built in a utility process.
  • Tags and filtering; export to .md through the save dialog (Native Dialogs and Files).
  • Drag a .md file onto the window to import it (Drag and Drop).
  • Encrypt notes marked "private" with a key from safeStorage.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is the Markdown source escaped before formatting rules add tags?

    Escaping first means the only tags in the output are those the renderer generates itself.
  2. 2. Why does NoteStore skip a corrupt note file when listing?

    Robust list operations treat each file independently. The bad one can be reported or quarantined separately.
  3. 3. Why keep note-store.js free of Electron imports?

    A plain module needs no running Electron, so tests are fast and the logic is portable.
  4. 4. How many IPC channels does the preload expose in this project?

    A small named surface is the security design: the page can only perform four validated actions.

Summary

  • A good Electron app is a thin Electron shell around plain, testable modules.
  • The main process owns files and validation; the preload exposes four named functions; the renderer only renders.
  • Store notes as one file each, with atomic saves, strict ids and tolerance for corrupt files.
  • Escape untrusted Markdown before rendering and add a CSP.
  • Finish with menus, settings, packaging, signing and updates.

Next: Project: A Clipboard History Tool