Skip to content

Commit aacdf54

Browse files
committed
Change shorthand syntax language to allow for correct Markdown syntax highlight
1 parent 95a1593 commit aacdf54

File tree

6 files changed

+15
-12
lines changed

6 files changed

+15
-12
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ Then go to Emera's settings and refresh components. Now you can use your `HelloW
7777

7878
To render component inline, add ```emera:<HelloWorldInline name="Obsidian" />```. Everything after `emera:` will be parsed as JSX, so you can set props, add children elements, etc.
7979

80-
To render component as block, there are two syntaxes. Shorthand syntax is convenient if your component acts as a simple wrapper (e.g. `<Callout />` kind of component). You add it by creating code block with language ```emera:<Name of your component>```
80+
To render component as block, there are two syntaxes. Shorthand syntax is convenient if your component acts as a simple wrapper (e.g. `<Callout />` kind of component). You add it by creating code block with language ```emmd:<Name of your component>```
8181

8282
````markdown
83-
```emera:RedCallout
83+
```emmd:RedCallout
8484
You can use **Markdown** inside `<RedCallout />`.
8585
```
8686
````

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "emera",
33
"name": "Emera",
4-
"version": "1.3.0",
4+
"version": "1.4.0",
55
"minAppVersion": "1.6.5",
66
"description": "Enables you to use custom React components and inline JavaScript, kinda like MDX.",
77
"author": "OlegWock",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-emera",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"description": "MDX for Obsidian. Kinda.",
55
"main": "main.js",
66
"scripts": {

src/consts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export const EMERA_ROOT_SCOPE = '_emeraRootScope';
33
export const EMERA_GET_SCOPE = '_emeraGetScope';
44

55
export const EMERA_JSX_LANG_NAME = 'emera';
6-
export const EMERA_COMPONENT_PREFIX = 'Em';
76
export const EMERA_JS_LANG_NAME = 'emjs';
87
export const EMERA_INLINE_JS_PREFIX = `${EMERA_JS_LANG_NAME}:`;
98
export const EMERA_INLINE_JSX_PREFIX = `${EMERA_JSX_LANG_NAME}:`;
9+
export const EMERA_JSX_SHORTHAND_LANG_NAME = 'emmd';

src/processors/code-processor.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { MarkdownPostProcessorContext, TFile, MarkdownView } from 'obsidian';
1616
import { EmeraPlugin } from '../plugin';
1717
import { iife } from '../utils';
1818
import { emeraCurrentEditorStateField, findCurrentView, isCursorBetweenNodes, isCursorOnSameLineWithNode } from './utils';
19-
import { EMERA_INLINE_JS_PREFIX, EMERA_INLINE_JSX_PREFIX, EMERA_JS_LANG_NAME, EMERA_JSX_LANG_NAME } from '../consts';
19+
import { EMERA_INLINE_JS_PREFIX, EMERA_INLINE_JSX_PREFIX, EMERA_JS_LANG_NAME, EMERA_JSX_LANG_NAME, EMERA_JSX_SHORTHAND_LANG_NAME } from '../consts';
2020
import { getAnonymousDocScope, getPageScope, getScope, ScopeNode } from '../scope';
2121
import { compileJsxIntoFactory, importFromString, transpileCode } from '../bundler';
2222
import { renderComponent } from '../renderer';
@@ -346,18 +346,20 @@ export class EmeraCodeProcessor {
346346
return;
347347
}
348348

349-
console.log('MD post', el, ctx);
349+
// console.log('MD post', el, ctx);
350350

351351
const file = ctx.sourcePath ? this.plugin.app.vault.getFileByPath(ctx.sourcePath) : null;
352352
const code = Array.from(el.querySelectorAll('code'));
353353
const toProcess = code.flatMap((el): ToProcessPreviewRecord[] => {
354354
const content = el.textContent ?? '';
355355
if (el.parentElement?.tagName.toLowerCase() === 'pre') {
356356
// Multi-line code block
357-
if (el.className.includes(`language-${EMERA_JSX_LANG_NAME}`)) {
358-
const regex = new RegExp(`language-${EMERA_JSX_LANG_NAME}:([\\S]+)`);
357+
console.log('Process el', el.className);
358+
if (el.className.includes(`language-${EMERA_JSX_LANG_NAME}`) || el.className.includes(`language-${EMERA_JSX_SHORTHAND_LANG_NAME}`)) {
359+
const regex = new RegExp(`language-(?:${EMERA_JSX_LANG_NAME}|${EMERA_JSX_SHORTHAND_LANG_NAME}):([\\S]+)`);
359360
const match = regex.exec(el.className);
360361
const componentSpecifier = match?.[1];
362+
console.log('Process el', el.className, 'match', match);
361363
return [{
362364
type: 'block-jsx',
363365
el,
@@ -504,15 +506,15 @@ export class EmeraCodeProcessor {
504506

505507
const isFenceStart = node.type.name.includes('HyperMD-codeblock-begin');
506508
const isFenceEnd = node.type.name.includes('HyperMD-codeblock-end');
507-
const containstEmeraSpecifier = nodeContent.trim().endsWith(EMERA_JSX_LANG_NAME) || nodeContent.trim().includes(`${EMERA_JSX_LANG_NAME}:`) || nodeContent.trim().endsWith(EMERA_JS_LANG_NAME);
509+
const containstEmeraSpecifier = nodeContent.trim().endsWith(EMERA_JSX_LANG_NAME) || nodeContent.trim().includes(`${EMERA_JSX_SHORTHAND_LANG_NAME}:`) || nodeContent.trim().endsWith(EMERA_JS_LANG_NAME);
508510

509511
if (isFenceStart && containstEmeraSpecifier && !currentBlockStartNode) {
510512
currentBlockStartNode = node.node;
511513
currentBlockStartType = nodeContent.trim().endsWith(EMERA_JS_LANG_NAME) ? 'block-js' : 'block-jsx';
512514
} else if (isFenceEnd && currentBlockStartNode) {
513515
const text = state.doc.sliceString(currentBlockStartNode.from, node.to).trim();
514516

515-
const regex = new RegExp(`([\`~]{3,})(?:${EMERA_JS_LANG_NAME}|${EMERA_JSX_LANG_NAME}:?(\\S+)?)\\n([\\s\\S]+)\\n\\1`);
517+
const regex = new RegExp(`([\`~]{3,})(?:${EMERA_JS_LANG_NAME}|(?:${EMERA_JSX_LANG_NAME}|${EMERA_JSX_SHORTHAND_LANG_NAME}):?(\\S+)?)\\n([\\s\\S]+)\\n\\1`);
516518
const match = regex.exec(text);
517519

518520
if (match) {

src/side-effects.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { EMERA_GET_SCOPE, EMERA_JS_LANG_NAME, EMERA_JSX_LANG_NAME, EMERA_MODULES, EMERA_ROOT_SCOPE } from "./consts";
1+
import { EMERA_GET_SCOPE, EMERA_JS_LANG_NAME, EMERA_JSX_LANG_NAME, EMERA_JSX_SHORTHAND_LANG_NAME, EMERA_MODULES, EMERA_ROOT_SCOPE } from "./consts";
22
import { registerCodemirrorMode } from './utils';
33
import { exposedModules } from "./exposed-modules";
44
import { getScope, ScopeNode } from './scope';
55

66
// Add syntax highlight for emera
77
registerCodemirrorMode(EMERA_JSX_LANG_NAME, 'jsx');
88
registerCodemirrorMode(EMERA_JS_LANG_NAME, 'javascript');
9+
registerCodemirrorMode(EMERA_JSX_SHORTHAND_LANG_NAME, 'markdown');
910

1011
// Expose modules
1112
(window as any)[EMERA_MODULES] = new Proxy({

0 commit comments

Comments
 (0)