Skip to content

Commit 5906447

Browse files
committed
Fixed auto direction in exports (#74).
1 parent 220c65a commit 5906447

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-5
lines changed

AutoDirPostProcessor.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,30 @@ function detectCanvasElement(el: HTMLElement, ctx: MarkdownPostProcessorContext,
4343
}
4444
}
4545

46+
// Try to detect if the postprocessor was asked to render an export, which seems to be the only case on which
47+
// the post processor is called with a top-level Markdown Preview View div.
48+
// In this case, we must add the relevant document direction class to that element, because no one else will.
49+
function detectExport(el: HTMLElement, ctx: MarkdownPostProcessorContext, setPreviewDirection: SetPreviewDirection) {
50+
if (el?.classList && el.classList?.contains('markdown-preview-view')) {
51+
setPreviewDirection(ctx.sourcePath, el as HTMLDivElement);
52+
}
53+
}
54+
4655
type SetPreviewDirection = (path: string, markdownPreviewElement: HTMLDivElement) => void;
4756

4857
/*
4958
* This Markdown post-processor handles the Reading view and other rendered components of notes.
5059
* It detects the direction for each node individually and adds corresponding CSS classes that are
5160
* later referenced in styles.css.
5261
*/
53-
export const autoDirectionPostProcessor = (el: HTMLElement, ctx: MarkdownPostProcessorContext, setPreviewDirection: SetPreviewDirection) => {
62+
export const autoDirectionPostProcessor = (
63+
el: HTMLElement,
64+
ctx: MarkdownPostProcessorContext,
65+
setPreviewDirection: SetPreviewDirection,
66+
) => {
5467
let shouldAddDir = false, addedDir = false;
5568
detectCanvasElement(el, ctx, setPreviewDirection);
69+
detectExport(el, ctx, setPreviewDirection);
5670

5771
// Obsidian renders adjacent lines as one <p> element with <br> breaks. Since these cannot
5872
// be set a direction individually, the following breaks them into individual divs.

README.md

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

8080
## Changelog
8181

82+
### 1.2.1
83+
84+
- Fixed auto direction in PDF exports (https://github.com/esm7/obsidian-rtl/issues/74).
85+
8286
### 1.2.0
8387

8488
- Auto direction is now the default for new users installing the plugin.

main.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default class RtlPlugin extends Plugin {
2828
this.registerEditorExtension(this.autoDirectionPlugin);
2929
this.registerEditorExtension(EditorView.perLineTextDirection.of(true));
3030
this.registerMarkdownPostProcessor((el, ctx) => {
31-
autoDirectionPostProcessor(el, ctx, (path, markdownPreviewElement) => this.setCanvasPreviewDirection(path, markdownPreviewElement));
31+
autoDirectionPostProcessor(el, ctx, (path, markdownPreviewElement) => this.setPreviewDirectionByFileSettings(path, markdownPreviewElement));
3232
});
3333

3434
await this.convertLegacySettings();
@@ -305,7 +305,11 @@ export default class RtlPlugin extends Plugin {
305305
readingDiv.classList.add('rtl-yaml');
306306
}
307307

308-
setCanvasPreviewDirection(path: string, markdownPreviewElement: HTMLDivElement) {
308+
// This method checks what's the required file direction for the given path and assigns its direction
309+
// classes accordingly.
310+
// It is needed for canvas and exports, on which there is no other parent element that includes the
311+
// global direction of the document, e.g. the 'is-auto' class.
312+
setPreviewDirectionByFileSettings(path: string, markdownPreviewElement: HTMLDivElement) {
309313
const file = this.app.vault.getAbstractFileByPath(path);
310314
const [requiredDirection, _] = this.getRequiredFileDirection(file);
311315
this.setDocumentDirectionForReadingDiv(markdownPreviewElement, requiredDirection);

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": "1.2.0",
4+
"version": "1.2.1",
55
"minAppVersion": "0.15.3",
66
"description": "Right to Left (RTL) text direction support for languages like Arabic, Hebrew and Persian (Farsi).",
77
"author": "esm",

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": "1.2.0",
3+
"version": "1.2.1",
44
"description": "RTL (Right to Left) language support for Obsidian.",
55
"main": "main.js",
66
"scripts": {

0 commit comments

Comments
 (0)