Skip to content

Commit b8f783c

Browse files
committed
fix: browser tab title now uses real file name instead of content-derived auto-name
- Auto-naming from content scoped to Untitled.md files only - Files with a real name always reflect it in the browser tab - Fenced code block language tags (html-autorun etc) stripped before auto-name extraction - First meaningful heading/line used for auto-name, not raw content start
1 parent a3c02ff commit b8f783c

2 files changed

Lines changed: 48 additions & 11 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Tab Title Fix — Use File Name as Browser Tab
2+
3+
- Fixed: browser tab showing garbled content-derived name (e.g. "htmlau") instead of actual file name
4+
- Auto-naming from content now only applies to files still named "Untitled.md" (or "Untitled N.md")
5+
- Files with a real name (disk files, user-named files) always use their filename as the tab title
6+
- Fixed: fenced code block language tags (e.g. `html-autorun`) no longer leak into auto-derived names
7+
- Auto-naming now scans for the first meaningful heading/line after stripping code blocks
8+
9+
---
10+
11+
## Summary
12+
The workspace auto-naming feature was overriding the browser tab title with a truncated, letter-only snippet of the document content. For documents starting with `\`\`\`html-autorun` blocks, this produced names like "htmlau". The fix scopes auto-naming exclusively to genuinely untitled files and ensures real file names are always respected.
13+
14+
---
15+
16+
## 1. Tab Title Uses Actual File Name
17+
**Files:** `js/workspace.js`
18+
**What:** Added an early-exit guard in `autoNameFromContent()` that checks whether the file's current name matches the "Untitled" pattern (`/^untitled(\s*\d*)?\.md$/i`). If the file already has a real name, the function returns immediately without modifying the title.
19+
**Impact:** The browser tab and header chip now always reflect the real workspace file name for any named file.
20+
21+
## 2. Code Block Language Tags Stripped from Auto-Name
22+
**Files:** `js/workspace.js`
23+
**What:** Before extracting the auto-name candidate, fenced code blocks (` ```lang ... ``` `) are stripped from the content string. The first non-empty heading or line is then extracted from the remaining text.
24+
**Impact:** Prevents block language identifiers like `html-autorun`, `python`, or `bash` from becoming the file/tab name.
25+
26+
---
27+
28+
## Files Changed (1 total)
29+
30+
| File | Lines Changed | Type |
31+
|------|:---:|------|
32+
| `js/workspace.js` | +15 −10 | Bug fix — auto-naming scoped to untitled files only |

js/workspace.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,19 +1043,24 @@
10431043
var file = findFileById(workspace.activeFileId);
10441044
if (!file) return;
10451045

1046-
var content = M.markdownEditor.value || '';
1047-
var stripped = content.replace(/^#+\s*/, '').trim(); // strip leading # headings
1046+
// Only auto-name files that are still genuinely untitled.
1047+
// Any file with a real name (set by user or loaded from storage) keeps it.
1048+
var baseName = file.name.split('/').pop();
1049+
if (!/^untitled(\s*\d*)?\.md$/i.test(baseName)) return;
10481050

1049-
if (!stripped) {
1050-
// Empty content → revert to Untitled
1051-
if (file.name !== 'Untitled.md') {
1052-
file.name = 'Untitled.md';
1053-
saveWorkspace();
1054-
renderFileList();
1055-
updatePageTitle(file.name);
1056-
}
1057-
return;
1051+
var content = M.markdownEditor.value || '';
1052+
// Strip fenced code blocks so ```html-autorun doesn't leak into the name
1053+
var strippedContent = content.replace(/^```[^\n]*\n[\s\S]*?```\n?/gm, '').trim();
1054+
// Find first heading or non-empty line
1055+
var firstMeaningfulLine = '';
1056+
var lines = strippedContent.split('\n');
1057+
for (var li = 0; li < lines.length; li++) {
1058+
var l = lines[li].trim();
1059+
if (l) { firstMeaningfulLine = l; break; }
10581060
}
1061+
var stripped = firstMeaningfulLine.replace(/^#+\s*/, '').trim();
1062+
1063+
if (!stripped) return; // No content yet — keep "Untitled.md"
10591064

10601065
// Take first 10 chars, keep only ASCII letters and spaces
10611066
var autoName = stripped.substring(0, 10).replace(/[^a-zA-Z ]/g, '').trim();

0 commit comments

Comments
 (0)