Skip to content

Commit 08dedc0

Browse files
committed
Fixed an issue of direction not properly detected if RTL is the default
1 parent 5a52529 commit 08dedc0

File tree

4 files changed

+34
-20
lines changed

4 files changed

+34
-20
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ It is possible to temporarily override a note's direction regardless of the fron
6464

6565
## Changelog
6666

67+
### 0.2.2
68+
69+
- Fixed an issue of not properly detecting the direction when the default is set as RTL (due to Obsidian's own styles).
70+
6771
### 0.2.1
6872

6973
- Fixed an issue of detecting the legacy editor.

main.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,14 @@ export default class RtlPlugin extends Plugin {
170170
containerEl.classList.add('is-rtl');
171171
this.replacePageStyleByString('List indent fix',
172172
`/* List indent fix */ .cm-s-obsidian .HyperMD-list-line { text-indent: 0px !important; }`, true);
173+
// this.replaceStringInStyle('.markdown-source-view.mod-cm6 .cm-fold-indicator .collapse-indicator',
174+
// 'right: 0;', 'right: -15px;');
173175
} else {
174176
containerEl.classList.remove('is-rtl');
175177
this.replacePageStyleByString('List indent fix',
176178
`/* List indent fix */ /* Empty rule for LTR */`, true);
179+
// this.replaceStringInStyle('.markdown-source-view.mod-cm6 .cm-fold-indicator .collapse-indicator',
180+
// 'right: -15px;', 'right: 0;');
177181
}
178182
this.replacePageStyleByString('Embedded links always LTR',
179183
`/* Embedded links always LTR */ .embedded-backlinks { direction: ltr; }`, true);
@@ -211,33 +215,39 @@ export default class RtlPlugin extends Plugin {
211215

212216
// Returns true if a replacement was made
213217
replacePageStyleByString(searchString: string, newStyle: string, addIfNotFound: boolean) {
214-
let styles = document.head.getElementsByTagName('style');
215-
let found = false;
216218
let alreadyExists = false;
217-
for (let style of styles) {
218-
if (style.getText().includes(searchString)) {
219-
found = true;
220-
if (style.getText() === searchString)
221-
alreadyExists = true;
222-
else
223-
style.setText(newStyle);
224-
}
225-
}
226-
if (!found && addIfNotFound) {
219+
let style = this.findPageStyle(searchString);
220+
if (style) {
221+
if (style.getText() === searchString)
222+
alreadyExists = true;
223+
else
224+
style.setText(newStyle);
225+
} else if (addIfNotFound) {
227226
let style = document.createElement('style');
228227
style.textContent = newStyle;
229228
document.head.appendChild(style);
230229
}
231-
return found && !alreadyExists;
230+
return style && !alreadyExists;
231+
}
232+
233+
// Returns true if a replacement was made
234+
replaceStringInStyle(searchString: string, whatToReplace: string, replacement: string) {
235+
let style = this.findPageStyle(searchString);
236+
if (style && style.getText().includes(whatToReplace)) {
237+
const newText = style.getText().replace(whatToReplace, replacement);
238+
style.textContent = newText;
239+
return true;
240+
}
241+
return false;
232242
}
233243

234-
findPageStyle(searchString: string) {
244+
findPageStyle(regex: string) {
235245
let styles = document.head.getElementsByTagName('style');
236246
for (let style of styles) {
237-
if (style.getText().includes(searchString))
238-
return true;
247+
if (style.getText().match(regex))
248+
return style;
239249
}
240-
return false;
250+
return null;
241251
}
242252

243253
patchAutoCloseBrackets(cmEditor: any, newDirection: string) {
@@ -266,7 +276,7 @@ export default class RtlPlugin extends Plugin {
266276
var cmEditor = this.getCmEditor();
267277
return cmEditor?.getOption('direction') === 'rtl' ? 'rtl' : 'ltr';
268278
} else {
269-
return this.findPageStyle('direction: rtl') ? 'rtl' : 'ltr';
279+
return this.findPageStyle('New editor content div.*direction: rtl') ? 'rtl' : 'ltr';
270280
}
271281
}
272282

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "obsidian-rtl",
33
"name": "RTL Support",
4-
"version": "0.2.1",
4+
"version": "0.2.2",
55
"description": "Right to Left (RTL) text direction support for languages like Arabic, Hebrew and Farsi.",
66
"author": "esm",
77
"authorUrl": "",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-rtl",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "RTL (Right to Left) language support for Obsidian.",
55
"main": "main.js",
66
"scripts": {

0 commit comments

Comments
 (0)