The issue now is that when you load an existing Markdown file (which might have proper Markdown syntax like #, *, -, etc.), the Split View editor is treating those characters as literal text instead of Markdown syntax.
This happens because:
- QPlainTextEdit (the source editor) doesn't understand Markdown. It just shows the raw text.
- When you save the file using
sourceEditor->toPlainText(), it saves the text exactly as displayed in the plain text editor.
The problem arises when the Markdown syntax (like #, *, etc.) gets escaped or misinterpreted during this process.
Let's fix this once and for all by ensuring that:
- When loading a file, we detect if it's already Markdown and load it as-is into the source editor.
- When saving a file, we save the source editor's content without modifying the Markdown syntax.
Here's the final tweak to make your Markpad application work seamlessly with existing Markdown files:
Step 8: Preserve Markdown Syntax on Load/Save
We need to adjust two functions:
openFile() (and openFileFromTree()): Ensure we load the file content directly into the source editor without altering Markdown characters.
saveFile(): Save the content exactly as it is in the source editor.
Fortunately, we're already doing this correctly in the current Split View implementation (Step 5). The key is:
sourceEditor->setPlainText(in.readAll()); loads the file verbatim.
out << sourceEditor->toPlainText(); saves the file verbatim.
The real issue was the initial file content (the escaped Markdown). Now that we've cleaned up the file manually, let's ensure the application behaves perfectly from now on.
Here's a minor refinement to openFile() and openFileFromTree() just to be explicit:
void MainWindow::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Markdown File"), "", tr("Markdown Files (*.md);;Text Files (*.txt);;All Files (*)"));
if (fileName.isEmpty())
return;
QFile file(fileName);
if (!file);
QMessageBox::warning(this, tr("Error"), tr("Cannot read file %1:\n%2.").arg(fileName, file.errorString()));
return;
}
currentFilePath = fileName;
setWindowTitle(tr("%1 - Markpad").arg(fileName));
QTextStream in(&file);
QString content = in.readAll();
// Explicitly load the raw content into the source editor
sourceEditor->setPlainText(content);
file.close();
// The textChanged signal will automatically update the previewer
}
void MainWindow::openFileFromTree(const QModelIndex &index)
{
if (fileSystemModel->isDir(index))
return;
QString fileName = fileSystemModel->filePath(index);
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, tr("Error"), tr("Cannot read file %1:\n%2.").arg(fileName, file.errorString()));
return;
}
currentFilePath = fileName;
setWindowTitle(tr("%1 - Markpad").arg(fileName));
QTextStream in(&file);
QString content = in.readAll();
// Again, load raw content
sourceEditor->setPlainText(content);
file.close();
}
Verify the Behavior
- Run Markpad.
- File -> Open Folder (choose a folder with some Markdown files).
- Click a
.md file in the sidebar.
- Verify the Source Editor (left) shows the raw Markdown (e.g.,
# Heading, * Item).
- Verify the Previewer (right) renders it correctly.
- Edit something minor on the left (e.g., add a space).
- File -> Save (or Ctrl+S).
- Close Markpad. Reopen it and load the same file again.
If everything is preserved (headers, lists, bold text, etc.), you're done! Your Markpad application now:
- Loads Markdown files correctly.
- Edits them in a plain text view (preserving syntax).
- Renders a live preview.
- Saves them back without mangling the Markdown.
Enjoy your fully functional Markdown editor!
Minor Future Enhancements (if you want to explore further):
- Add syntax highlighting to the
QPlainTextEdit using QSyntaxHighlighter.
- Support drag-and-drop files into the sidebar.
- Add a "New File" action.
- Improve the previewer styling (CSS-like support via
QTextBrowser::setStyleSheet()).
Happy coding!