generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
187 lines (155 loc) · 6.28 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { App, Plugin, Notice, MarkdownView, WorkspaceLeaf, ItemView } from 'obsidian';
const syllable = require('syllable').default || require('syllable').syllable || require('syllable');
// Custom view for the sidebar panel
const VIEW_TYPE_SYLLABLE_COUNT = "syllable-count-view";
class SyllableCountView extends ItemView {
private contentEl: HTMLElement;
constructor(leaf: WorkspaceLeaf) {
super(leaf);
// Get the main content container
this.contentEl = this.containerEl.children[1];
}
// Return the unique view type for this view
getViewType() {
return VIEW_TYPE_SYLLABLE_COUNT;
}
// Return the display text for the view's tab
getDisplayText() {
return "Syllable Count";
}
// Return the icon to use for this view's tab
getIcon(): string {
return "music-2"; // Uses the built-in "music-2" icon
}
// Called when the view is opened
async onOpen() {
this.updateSyllableCounts();
}
// Update the syllable counts based on the current active note
async updateSyllableCounts() {
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!activeView) {
// If there's no active Markdown file, display a message
this.contentEl.empty();
this.contentEl.createEl("p", { text: "No active markdown file." });
return;
}
const editor = activeView.editor;
const lines = editor.getValue().split("\n"); // Split the note into lines
this.contentEl.empty(); // Clear existing content
let inFrontmatter = false; // Track whether we're inside frontmatter
let itemCount = 0; // Count of valid lines
let lastItemIndex = -1; // Index of the last valid line
// First, count the valid items to determine the last item index
lines.forEach((line) => {
const trimmedLine = line.trim();
// Toggle frontmatter tracking on/off when encountering "---"
if (trimmedLine === "---") {
inFrontmatter = !inFrontmatter;
return;
}
// Skip lines that are inside frontmatter, empty, or special Markdown syntax
if (
inFrontmatter ||
trimmedLine === "" ||
trimmedLine.startsWith("#") ||
trimmedLine.startsWith("```") ||
trimmedLine.startsWith(">")
) {
return;
}
// Increment itemCount for valid lines
itemCount++;
lastItemIndex++;
});
// Reset frontmatter tracking for the second pass
inFrontmatter = false;
let currentItemIndex = 0; // Index to keep track of valid items
// Now, generate the syllable count items
lines.forEach((line, index) => {
const trimmedLine = line.trim();
// Toggle frontmatter tracking on/off when encountering "---"
if (trimmedLine === "---") {
inFrontmatter = !inFrontmatter;
return;
}
// Skip lines that are inside frontmatter, empty, or special Markdown syntax
if (
inFrontmatter ||
trimmedLine === "" ||
trimmedLine.startsWith("#") ||
trimmedLine.startsWith("```") ||
trimmedLine.startsWith(">")
) {
return;
}
const count = syllable(line); // Get the syllable count for the line
const lineText = trimmedLine === "" ? "(empty line)" : line;
// Create a styled container for each line's syllable count
const lineItem = this.contentEl.createEl("div", { cls: "syllable-count-item" });
lineItem.createEl("span", { cls: "syllable-line-number", text: `Line ${index + 1}: ` });
lineItem.createEl("span", { cls: "syllable-line-text", text: lineText });
lineItem.createEl("span", { cls: "syllable-count-value", text: `(${count} syllables)` });
// Add "last-item" class to the last valid line item
if (currentItemIndex === lastItemIndex) {
lineItem.addClass("last-item");
}
currentItemIndex++;
});
}
}
export default class LyricistHelperPlugin extends Plugin {
async onload() {
console.log('Loading Lyricist Helper plugin...');
// Register the custom view type with Obsidian
this.registerView(
VIEW_TYPE_SYLLABLE_COUNT,
(leaf) => new SyllableCountView(leaf)
);
// Add a command to show the syllable count view
this.addCommand({
id: 'show-syllable-count-view',
name: 'Show Syllable Count view',
callback: () => this.activateView()
});
// Update syllable counts whenever the active leaf or content changes
this.registerEvent(
this.app.workspace.on('active-leaf-change', () => this.updateView())
);
this.registerEvent(
this.app.workspace.on('editor-change', () => this.updateView())
);
// Automatically activate the view on plugin load
this.activateView();
// Load external styles
this.loadStyles();
}
onunload() {
console.log('Unloading Lyricist Helper plugin...');
}
// Activate the syllable count view if it's not already active
async activateView() {
const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_SYLLABLE_COUNT);
if (leaves.length === 0) {
await this.app.workspace.getRightLeaf(false).setViewState({
type: VIEW_TYPE_SYLLABLE_COUNT,
});
}
this.updateView();
}
// Update the syllable count view
async updateView() {
const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_SYLLABLE_COUNT);
if (leaves.length > 0) {
const view = leaves[0].view as SyllableCountView;
view.updateSyllableCounts();
}
}
// Load styles from an external file
loadStyles() {
const link = document.createElement("link");
link.rel = "stylesheet";
link.href = this.getAssetPath("styles.css");
document.head.appendChild(link);
}
}