-
Notifications
You must be signed in to change notification settings - Fork 2
/
content.js
92 lines (81 loc) · 3.25 KB
/
content.js
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
// Throttle the applyRtlToPersianText function to run at most once every 500ms,
// preventing performance lag during rapid DOM changes.
let rtlApplyTimeout = null;
function applyRtlToPersianTextThrottled() {
if (rtlApplyTimeout) {
clearTimeout(rtlApplyTimeout);
}
rtlApplyTimeout = setTimeout(() => {
applyRtlToPersianText();
rtlApplyTimeout = null;
}, 500);
}
function applyRtlToPersianText() {
const persianRegex = /[\u0600-\u06FF]/;
const elements = document.querySelectorAll(
'div[contenteditable="true"] span, ' +
'div[contenteditable="true"] p, ' +
'div[contenteditable="true"] div, ' +
'div[contenteditable="true"] ul, ' +
'div[contenteditable="true"] ol, ' +
'div[contenteditable="true"] li, ' +
'div[contenteditable="true"] .notion-toggle, ' +
'div[contenteditable="true"] .notion-toggle-content'
);
elements.forEach(element => {
if (persianRegex.test(element.textContent)) {
element.style.direction = 'rtl';
element.style.textAlign = 'right';
} else {
element.style.direction = 'ltr';
element.style.textAlign = 'left';
}
});
// Apply RTL/LTR on numbered list blocks and pseudoBefore element (which shows the list number)
const numberedListElements = document.querySelectorAll('.notion-numbered_list-block');
numberedListElements.forEach(element => {
if (persianRegex.test(element.textContent)) {
const pseudoBefore = element.querySelector('.pseudoBefore');
if (pseudoBefore) {
pseudoBefore.style.direction = 'rtl';
pseudoBefore.style.textAlign = 'right';
}
} else {
const pseudoBefore = element.querySelector('.pseudoBefore');
if (pseudoBefore) {
pseudoBefore.style.direction = 'ltr';
pseudoBefore.style.textAlign = 'left';
}
}
});
// Force math elements to LTR by setting dir="ltr"
const mathElements = document.querySelectorAll('.katex, .notion-text-equation-token');
mathElements.forEach(elem => {
elem.setAttribute('dir', 'ltr');
elem.style.direction = 'ltr';
elem.style.textAlign = 'left';
elem.style.unicodeBidi = 'bidi-override';
});
// Adjust border and padding for RTL layout in quote blocks
const quoteBlocks = document.querySelectorAll('.notion-quote-block blockquote');
quoteBlocks.forEach(block => {
const quoteContent = block.querySelector('div:first-of-type');
if (!quoteContent) return;
if (persianRegex.test(quoteContent.textContent)) {
quoteContent.style.borderRight = '3px solid currentcolor';
quoteContent.style.borderLeft = 'none';
quoteContent.style.paddingRight = '14px';
quoteContent.style.paddingLeft = '0';
} else {
quoteContent.style.borderRight = 'none';
quoteContent.style.borderLeft = '3px solid currentcolor';
quoteContent.style.paddingRight = '0';
quoteContent.style.paddingLeft = '14px';
}
});
}
// Run the function on page load
document.addEventListener('DOMContentLoaded', applyRtlToPersianTextThrottled);
// Run the function every time the DOM changes
const observer = new MutationObserver(applyRtlToPersianTextThrottled);
observer.observe(document.body, { childList: true, subtree: true });