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
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@
"default": 50,
"minimum": 5,
"markdownDescription": "プレビューパネルに表示されるタブタイトルを指定した文字数で切り詰めます。"
},
"zenn-preview.sortArticle": {
"type": "string",
"default": null,
"enum": [
null,
"title"
],
"enumDescriptions": [
"ファイルパスの昇順で表示",
"タイトルの昇順で表示"
],
"markdownDescription": "記事の並び順を設定します。"
}
}
}
Expand Down
41 changes: 39 additions & 2 deletions src/treeview/previewTreeItem.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import naturalCompare from "natural-compare-lite";
import * as vscode from "vscode";

import { AppContext } from "../context/app";
import { ContentError } from "../schemas/error";
import { Contents, ContentsType } from "../types";
import { EMOJI_REGEX } from "../utils/patterns";

/** TreeItem の `contextValue` に設定できる値 */
export type TreeItemType = ContentsType | "error" | "none";
Expand Down Expand Up @@ -64,6 +64,43 @@ export abstract class PreviewTreeItem extends vscode.TreeItem {
* TreeItemをソートする
*/
static sortTreeItems(items: PreviewTreeItem[]): PreviewTreeItem[] {
return items.sort((a, b) => naturalCompare(a.path, b.path));
// 設定からソート順を取得
const sortArticle = vscode.workspace
.getConfiguration("zenn-preview")
.get<string | null>("sortArticle");

return items.sort((a, b) => {
// 記事タイトルでソート
if (sortArticle === "title") {
const getCleanLabel = (item: PreviewTreeItem): string => {
const labelProp = item.label;
let labelStr = "";
if (typeof labelProp === "string") {
labelStr = labelProp;
} else if (labelProp && typeof labelProp.label === "string") {
labelStr = labelProp.label;
}
return labelStr.replace(EMOJI_REGEX, "").trim();
};

const aCleanLabel = getCleanLabel(a);
const bCleanLabel = getCleanLabel(b);

if (aCleanLabel || bCleanLabel) {
Copy link

Copilot AI Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider using an explicit non-empty string check (e.g., aCleanLabel !== '' || bCleanLabel !== '') to clarify the intended condition and improve code readability for future maintainers.

Suggested change
if (aCleanLabel || bCleanLabel) {
if (aCleanLabel !== '' || bCleanLabel !== '') {

Copilot uses AI. Check for mistakes.

// どちらかに絵文字除去後ラベルがあれば比較
return aCleanLabel.localeCompare(bCleanLabel, "ja", {
sensitivity: "base",
});
}

// 絵文字除去後ラベルが両方空の場合はpathで比較 (元々ラベルがなかった場合など)
return a.path.localeCompare(b.path, "ja", { sensitivity: "base" });
}

// ファイルパスでソート
else {
return a.path.localeCompare(b.path, "ja", { sensitivity: "base" });
}
});
}
}