mirror of
https://github.com/mayurankv/Obsidian-Code-Styler.git
synced 2026-07-22 08:10:29 +00:00
update
This commit is contained in:
parent
7c5e83f4ed
commit
3d9b93dea6
10 changed files with 283 additions and 17 deletions
4
.todo
4
.todo
|
|
@ -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
|
||||
|
|
|
|||
144
main.js
144
main.js
File diff suppressed because one or more lines are too long
|
|
@ -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
|
||||
|
|
@ -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"}}
|
||||
|
|
@ -1 +0,0 @@
|
|||
18.17.0
|
||||
|
|
@ -1 +0,0 @@
|
|||
{"title":".nvmrc","rawUrl":"https://gitlab.com/gitlab-org/gitlab/-/raw/master/.nvmrc","datetime":"2024-02-14 03:40","displayUrl":"https://gitlab.com/gitlab-org/gitlab/-/blob/master/.nvmrc","author":"gitlab-org","repository":"gitlab","path":".nvmrc","fileName":".nvmrc","refInfo":{"ref":"","type":"","hash":"38f241024e79d3e5e7ce3d74c83186aa007f3ae3"}}
|
||||
|
|
@ -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() {}
|
||||
},
|
||||
|
|
@ -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}:{})});
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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": "",
|
||||
|
|
|
|||
12
src/main.ts
12
src/main.ts
|
|
@ -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
|
||||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in a new issue