Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mayurankv committed Feb 14, 2024
1 parent 7c5e83f commit 3d9b93d
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 17 deletions.
4 changes: 4 additions & 0 deletions .todo
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Features:
Other:
☐ YAML Frontmatter Highlighting
☐ https://raw.githubusercontent.com/deathau/cm-editor-syntax-highlight-obsidian/main/mode/yaml-frontmatter/yaml-frontmatter.js

Code Referencing:
☐ reference codeblock syntax highlighting
☐ Local reference
Expand Down
144 changes: 133 additions & 11 deletions main.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# vscode
.vscode

# Intellij
*.iml
.idea

# npm
node_modules

# Don't include the compiled main.js file in the repo.
# They should be uploaded to GitHub releases instead.
main.js

# Exclude sourcemaps
*.map

# obsidian
data.json

# Exclude macOS Finder (System Explorer) View States
.DS_Store
*.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":".gitignore","rawUrl":"https://github.com/twibiral/obsidian-execute-code/raw/master/.gitignore","datetime":"2024-02-14 05:57","displayUrl":"https://github.com/twibiral/obsidian-execute-code/blob/master/.gitignore","author":"twibiral","repository":"obsidian-execute-code","path":".gitignore","fileName":".gitignore","refInfo":{"ref":"master","type":"branch","hash":"6ee175e5cd802eafe034d6a9c68d39d831139818"}}

This file was deleted.

This file was deleted.

108 changes: 107 additions & 1 deletion src/EditingView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export function createCodeblockCodeMirrorExtensions(settings: CodeStylerSettings

const interaction = ViewPlugin.fromClass(
class ExamplePlugin implements PluginValue {
constructor() {} // view: EditorView
constructor() {
addReferenceSyntaxHighlight();
addYamlFrontmatterSyntaxHighlighting();
} // view: EditorView
update() {} // update: ViewUpdate
destroy() {}
},
Expand Down Expand Up @@ -551,6 +554,109 @@ function modeHighlight({start,text,language}: {start: number, text: string, lang
}
}
}
function addReferenceSyntaxHighlight() {
window.CodeMirror.defineMode("reference", function (config, parserConfig) {
const keyPattern = /^([a-zA-Z0-9_-]+)\s*(?=:)/;
const valuePattern = /^(:?(\s*(?:"(?:\\.|[^"])*"|\S+))?)/;
return {
startState: () => { return { inBlock: false, indent: 0 }; },
indent: (state, textAfter) => {
const indentUnit = config.indentUnit;
const indent = state.indent || 0;
if (textAfter && textAfter.charAt(0) === "-") return indent + indentUnit;
else return indent;
},
token: (stream, state) => {
if (stream.eatSpace()) return null;
if (stream.match(/^#.*/)) return "comment";
if (stream.match(/^https?:\/\/.*/)) return "variable";
if (stream.match(/\[\[.+\]\]/)) return "variable";
if (stream.match(keyPattern)) return "string";
if (stream.match(/:/)) return "meta";
const valueMatch = stream.match(valuePattern);
if (valueMatch) {
if (valueMatch[2].match(/("?)\/.*\/\1/)) return "operator";
if (valueMatch[2].startsWith("\"")) {
stream.skipToEnd(); // Consume the rest of the string
return "property";
} else if (/^\d+(\.\d+)?\b/.test(valueMatch[2])) return "number";
else if (/^(true|false|null)\b/.test(valueMatch[2])) return "atom";
else return null;
}

if (stream.sol()) {
const indent = stream.indentation();
if (indent > state.indent) {
state.indent = indent;
return "indent";
} else if (indent < state.indent) {
state.indent = indent;
return "dedent";
}
}

stream.next();
return null;
}
};
});
window.CodeMirror.defineMIME("text/reference", "reference");
}
function addYamlFrontmatterSyntaxHighlighting() {
const START = 0, FRONTMATTER = 1, BODY = 2;
window.CodeMirror.defineMode("yaml-frontmatter", function(config, parserConfig) {
const yamlMode = window.CodeMirror.getMode(config, "yaml");
const innerMode = window.CodeMirror.getMode(config, parserConfig && parserConfig.base || "gfm");

function curMode(state) {
return state.state == BODY ? innerMode : yamlMode;
}

return {
startState: function () {
return {
state: START,
inner: window.CodeMirror.startState(yamlMode)
};
},
copyState: function (state) {
return {
state: state.state,
inner: window.CodeMirror.copyState(curMode(state), state.inner)
};
},
token: function (stream, state) {
if (state.state == START) {
if (stream.match(/---/, false)) {
state.state = FRONTMATTER;
return yamlMode.token(stream, state.inner);
} else {
state.state = BODY;
state.inner = window.CodeMirror.startState(innerMode);
return innerMode.token(stream, state.inner);
}
} else if (state.state == FRONTMATTER) {
const end = stream.sol() && stream.match(/(---|\.\.\.)/, false);
const style = yamlMode.token(stream, state.inner);
if (end) {
state.state = BODY;
state.inner = window.CodeMirror.startState(innerMode);
}
return style;
} else {
return innerMode.token(stream, state.inner);
}
},
innerMode: function (state) {
return {mode: curMode(state), state: state.inner};
},
blankLine: function (state) {
const mode = curMode(state);
if (mode.blankLine) return mode.blankLine(state.inner);
}
};
});
}

export function editingDocumentFold(view: EditorView, toFold?: boolean) {
view.dispatch({effects: foldAll.of((typeof toFold !== "undefined")?{toFold: toFold}:{})});
Expand Down
2 changes: 1 addition & 1 deletion src/Referencing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function getReference(codeblockLines: Array<string>, sourcePath: st
const externalReferenceId = idExternalReference(reference.path);
reference.external = {
website: externalReferenceId.website,
storePath: EXTERNAL_REFERENCE_PATH + externalReferenceId.id,
storePath: plugin.app.vault.configDir + EXTERNAL_REFERENCE_PATH + externalReferenceId.id,
info: {title: "", url: reference.path, site: externalReferenceId.website, datetime: "", rawUrl: reference.path},
};
referenceParameters.filePath = await accessExternalReference(reference, plugin);
Expand Down
4 changes: 2 additions & 2 deletions src/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,11 +667,11 @@ const settingsUpdaters: Record<string,(settings: CodeStylerSettings)=>CodeStyler
export const FOLD_PLACEHOLDER = "Folded Code";
export const PARAMETERS = ["title","fold","ln","wrap","unwrap","ignore"];
export const TRANSITION_LENGTH = 240; // 240ms
export const SPECIAL_LANGUAGES = ["^reference$","^preview$","^include$","^output$","^run-.+$"];
export const SPECIAL_LANGUAGES = ["^reference$","^foofoo","^preview$","^include$","^output$","^run-.+$"];
export const SETTINGS_SOURCEPATH_PREFIX = "@Code-Styler-Settings:";
export const LOCAL_PREFIX = "@/";
export const REFERENCE_CODEBLOCK = "reference";
export const EXTERNAL_REFERENCE_PATH = ".obsidian/plugins/code-styler/reference-files/";
export const EXTERNAL_REFERENCE_PATH = "/plugins/code-styler/reference-files/";
export const EXTERNAL_REFERENCE_INFO_SUFFIX = "-info.json";
export const GIT_ICONS: { [key: string]: string } = {
"branch": "&#xe0a0;",
Expand Down
12 changes: 12 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export default class CodeStylerPlugin extends Plugin {

this.executeCodeMutationObserver = executeCodeMutationObserver; // Add execute code mutation observer

//@ts-expect-error Undocumented Obsidian API
window.CodeMirror.modeInfo.push({
name: "reference",
mime: "text/reference",
mode: "reference",
ext: ["reference"]
});
this.registerMarkdownCodeBlockProcessor(REFERENCE_CODEBLOCK, async (source, el, ctx) => { await referenceCodeblockProcessor(source, el, ctx, this);});

this.registerMarkdownPostProcessor(async (el,ctx) => {await readingViewCodeblockDecoratingPostProcessor(el,ctx,this);}); // Add codeblock decorating markdownPostProcessor
Expand Down Expand Up @@ -106,6 +113,11 @@ export default class CodeStylerPlugin extends Plugin {
}

onunload() {
//@ts-expect-error Undocumented Obsidian API
const referenceModeIndex = window.CodeMirror.modeInfo.findIndex((mode) => mode.mode === "reference");
if (referenceModeIndex !== -1)
//@ts-expect-error Undocumented Obsidian API
window.CodeMirror.modeInfo.splice(referenceModeIndex, 1);
this.executeCodeMutationObserver.disconnect();
removeStylesAndClasses();
destroyReadingModeElements();
Expand Down

0 comments on commit 3d9b93d

Please sign in to comment.