diff --git a/novelwriter/core/project.py b/novelwriter/core/project.py index e86b744b..3e3152ee 100644 --- a/novelwriter/core/project.py +++ b/novelwriter/core/project.py @@ -224,7 +224,7 @@ class NWProject: return True - def copyFileContent(self, tHandle: str, sHandle: str) -> bool: + def copyFileContent(self, tHandle: str, sHandle: str, newTitle: str | None = None) -> bool: """Copy content to a new document after it is created. This will not run if the file exists and is not empty. """ @@ -239,6 +239,14 @@ class NWProject: logger.debug("Populating '%s' with text from '%s'", tHandle, sHandle) text = self._storage.getDocumentText(sHandle) + if ( + newTitle and (lines := text.split("\n")) and lines + and lines[0].startswith(("# ", "## ", "### ", "#### ", "#! ", "##! ", "###! ")) + ): + prefix, _, _ = lines[0].partition(" ") + lines[0] = f"{prefix} {newTitle}" + text = "\n".join(lines) + self._storage.getDocument(tHandle).writeDocument(text) sItem.setLayout(tItem.itemLayout) self._index.reIndexHandle(tHandle) diff --git a/novelwriter/gui/projtree.py b/novelwriter/gui/projtree.py index 4565e315..b35445ae 100644 --- a/novelwriter/gui/projtree.py +++ b/novelwriter/gui/projtree.py @@ -667,7 +667,7 @@ class GuiProjectTree(QTreeView): if itemType == nwItemType.FILE: if tHandle := SHARED.project.newFile(newLabel, sHandle, sPos): if copyDoc: - SHARED.project.copyFileContent(tHandle, copyDoc) + SHARED.project.copyFileContent(tHandle, copyDoc, newLabel) elif hLevel > 0: SHARED.project.writeNewFile(tHandle, hLevel, not nNote) SHARED.project.index.reIndexHandle(tHandle)