Skip to content

Commit

Permalink
feat: better option for DJS convertion (allow to choose which one to …
Browse files Browse the repository at this point in the history
…export, like inlineDQL, inlineDJS...)
  • Loading branch information
Mara-Li committed Jul 30, 2024
1 parent 295797c commit e3b4ab5
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 50 deletions.
23 changes: 20 additions & 3 deletions src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,26 @@
"to": "To"
},
"convertDataview": {
"desc": "Allows you to convert dataview queries (inline DQL, Djs...) to markdown. \nDoes not always work, use with moderation.",
"title": "Convert Dataview queries to markdown"
"block": "Code block",
"djs": {
"block": {
},
"inline": {
},
"title": "Dataview JS"
},
"dql": {
"block": {
},
"inline": {
},
"title": "Dataview Query Language"
},
"global": {
"desc": "Allows you to convert dataview queries (inline DQL, Djs...) to markdown. \nDoes not always work, use with moderation.",
"title": "Convert Dataview queries to markdown"
},
"inline": "Inline"
},
"copyAsHTML": "Copy as HTML instead of Markdown. Some options may not be available.",
"copyLinksAsText": {
Expand All @@ -45,7 +63,6 @@
"remove": "Remove markup for all links",
"title": "Links format"
},
"dataview": "Failed to convert Dataview query. \nPlease update the Dataview module to the latest version.",
"debug": {
"desc": "Show debug log in console",
"title": "Debug information"
Expand Down
23 changes: 20 additions & 3 deletions src/i18n/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,26 @@
"to": "Vers"
},
"convertDataview": {
"desc": "Permet de convertir les queries dataview (inline DQL, Djs...) en markdown. Ne fonctionne pas toujours, à utiliser avec modération.",
"title": "Convertir les queries Dataview en markdown"
"block": "Bloc de code",
"djs": {
"block": {
},
"inline": {
},
"title": "Dataview JS"
},
"dql": {
"block": {
},
"inline": {
},
"title": "Dataview Query Language"
},
"global": {
"desc": "Permet de convertir les queries dataview (inline DQL, Djs...) en markdown. Ne fonctionne pas toujours, à utiliser avec modération.",
"title": "Convertir les queries Dataview en markdown"
},
"inline": "Inline"
},
"copyAsHTML": "Copier en HTML au lieu de Markdown. Certaines options peuvent ne pas être disponibles.",
"copyLinksAsText": {
Expand All @@ -45,7 +63,6 @@
"remove": "Supprimer les balises pour tous les liens",
"title": "Format des liens"
},
"dataview": "Impossible de convertir la requête Dataview. Veuillez mettre à jour le module Dataview à la dernière version.",
"debug": {
"desc": "Affiche le journal de débogage dans la console",
"title": "Informations de débogage"
Expand Down
38 changes: 37 additions & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,49 @@ export interface AutoRules {
value: string;
}

export interface ConvertDataview {
enable: boolean;
djs: {
inline: boolean;
block: boolean;
};
dql: {
inline: boolean;
block: boolean;
};
}

export const DEFAULT_DATAVIEW_SETTINGS_DISABLED: ConvertDataview = {
enable: false,
djs: {
inline: false,
block: false,
},
dql: {
inline: false,
block: false,
},
};

export const DEFAULT_DATAVIEW_SETTINGS_ENABLED: ConvertDataview = {
enable: true,
djs: {
inline: true,
block: true,
},
dql: {
inline: true,
block: true,
},
};

export interface GlobalSettings {
name?: string;
copyAsHTML?: boolean;
footnotes: ConversionOfFootnotes;
links: ConversionOfLinks;
callout: CalloutKeepType;
convertDataview?: boolean;
convertDataview?: ConvertDataview;
highlight: boolean;
hardBreak: boolean;
replaceText: ReplaceText[];
Expand Down
89 changes: 82 additions & 7 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {

import {
ApplyingToView,
DEFAULT_DATAVIEW_SETTINGS_DISABLED,
DEFAULT_DATAVIEW_SETTINGS_ENABLED,
type CalloutKeepType,
type ConversionOfFootnotes,
type ConversionOfLinks,
Expand Down Expand Up @@ -105,16 +107,89 @@ export class EnhancedCopySettingTab extends PluginSettingTab {
new Setting(this.settingsPage).setName(i18next.t("edit.desc")).setHeading();

if (this.app.plugins.enabledPlugins.has("dataview")) {
if (!settings.convertDataview)
settings.convertDataview = structuredClone(DEFAULT_DATAVIEW_SETTINGS_DISABLED);
//@ts-ignore
else if (settings.convertDataview === true)
settings.convertDataview = structuredClone(DEFAULT_DATAVIEW_SETTINGS_ENABLED);

new Setting(this.settingsPage)
.setName(i18next.t("convertDataview.title"))
.setDesc(i18next.t("convertDataview.desc"))
.setName(i18next.t("convertDataview.global.title"))
.setDesc(i18next.t("convertDataview.global.desc"))
.addToggle((toggle) => {
toggle.setValue(settings.convertDataview ?? false).onChange(async (value) => {
settings.convertDataview = value;
await this.plugin.saveSettings();
this.renderSettingsPage(settings.name ?? "edit");
});
toggle
.setValue(settings.convertDataview?.enable ?? false)
.onChange(async (value) => {
if (value)
settings.convertDataview = structuredClone(
DEFAULT_DATAVIEW_SETTINGS_ENABLED
);
else
settings.convertDataview = structuredClone(
DEFAULT_DATAVIEW_SETTINGS_DISABLED
);
await this.plugin.saveSettings();
this.renderSettingsPage(settings.name ?? "edit");
});
});

if (settings.convertDataview?.enable) {
new Setting(this.settingsPage)
.setHeading()
.setClass("dataview")
.setName(i18next.t("convertDataview.dql.title"));
new Setting(this.settingsPage)
.setName(i18next.t("convertDataview.block"))
.setClass("dataview")
.addToggle((toggle) => {
toggle
.setValue(settings.convertDataview?.djs.block ?? false)
.onChange(async (value) => {
settings.convertDataview!.djs.block = value;
await this.plugin.saveSettings();
});
});
new Setting(this.settingsPage)
.setName(i18next.t("convertDataview.inline"))
.setClass("dataview")
.addToggle((toggle) => {
toggle
.setValue(settings.convertDataview?.djs.inline ?? false)
.onChange(async (value) => {
settings.convertDataview!.djs.inline = value;
await this.plugin.saveSettings();
this.renderSettingsPage(settings.name ?? "edit");
});
});
new Setting(this.settingsPage)
.setHeading()
.setClass("dataview")
.setName(i18next.t("convertDataview.djs.title"));
new Setting(this.settingsPage)
.setName(i18next.t("convertDataview.block"))
.setClass("dataview")
.addToggle((toggle) => {
toggle
.setValue(settings.convertDataview?.dql.block ?? false)
.onChange(async (value) => {
settings.convertDataview!.dql.block = value;
await this.plugin.saveSettings();
});
});

new Setting(this.settingsPage)
.setName(i18next.t("convertDataview.inline"))
.setClass("dataview")
.addToggle((toggle) => {
toggle
.setValue(settings.convertDataview?.dql.inline ?? false)
.onChange(async (value) => {
settings.convertDataview!.dql.inline = value;
await this.plugin.saveSettings();
this.renderSettingsPage(settings.name ?? "edit");
});
});
}
}

new Setting(this.settingsPage)
Expand Down
10 changes: 10 additions & 0 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,13 @@
.enhanced-copy input {
font-family: var(--font-monospace);
}

.enhanced-copy .dataview:not(.setting-item.setting-item-heading) {
position: relative;
left: 20px;
}

.enhanced-copy .setting-item.setting-item-heading.dataview {
position: relative;
left: 10px;
}
83 changes: 47 additions & 36 deletions src/utils/dataview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ export async function convertDataviewQueries(
let replacedText = text;
const dataViewRegex = /```dataview\s(.+?)```/gms;
const isDataviewEnabled = app.plugins.plugins.dataview;
if (!isDataviewEnabled || !isPluginEnabled(app)) return replacedText;
if (!isDataviewEnabled || !isPluginEnabled(app) || !settings.convertDataview?.enable)
return replacedText;
const dvApi = getAPI(app);
if (!dvApi || dvApi === undefined) return replacedText;
const matches = text.matchAll(dataViewRegex);
Expand All @@ -192,49 +193,59 @@ export async function convertDataviewQueries(
/**
* DQL Dataview - The SQL-like Dataview Query Language
*/
for (const queryBlock of matches) {
try {
const block = queryBlock[0];
const markdown = await compiler.dataviewDQL(queryBlock[1]);
replacedText = replacedText.replace(block, markdown);
} catch (e) {
console.error(e);
return queryBlock[0];
if (settings.convertDataview?.dql?.block) {
for (const queryBlock of matches) {
try {
const block = queryBlock[0];
const markdown = await compiler.dataviewDQL(queryBlock[1]);
replacedText = replacedText.replace(block, markdown);
} catch (e) {
console.error(e);
return queryBlock[0];
}
}
}

for (const queryBlock of dataviewJsMatches) {
try {
const block = queryBlock[0];
const markdown = await compiler.dataviewJS(queryBlock[1]);
replacedText = replacedText.replace(block, markdown);
} catch (e) {
console.error(e);
return queryBlock[0];
/**
* DataviewJS - JavaScript API for Dataview
*/
if (settings.convertDataview?.djs?.block) {
for (const queryBlock of dataviewJsMatches) {
try {
const block = queryBlock[0];
const markdown = await compiler.dataviewJS(queryBlock[1]);
replacedText = replacedText.replace(block, markdown);
} catch (e) {
console.error(e);
return queryBlock[0];
}
}
}

//Inline queries
for (const inlineQuery of inlineMatches) {
try {
const code = inlineQuery[0];
const query = inlineQuery[1].trim();
const markdown = await compiler.inlineDQLDataview(query);
replacedText = replacedText.replace(code, markdown);
} catch (e) {
console.error(e);
return inlineQuery[0];
if (settings.convertDataview?.dql?.inline) {
for (const inlineQuery of inlineMatches) {
try {
const code = inlineQuery[0];
const query = inlineQuery[1].trim();
const markdown = await compiler.inlineDQLDataview(query);
replacedText = replacedText.replace(code, markdown);
} catch (e) {
console.error(e);
return inlineQuery[0];
}
}
}

for (const inlineJsQuery of inlineJsMatches) {
try {
const code = inlineJsQuery[0];
const markdown = await compiler.inlineDataviewJS(inlineJsQuery[1].trim());
replacedText = replacedText.replace(code, markdown);
} catch (e) {
console.error(e);
return inlineJsQuery[0];
//Inline JS queries
if (settings.convertDataview?.djs?.inline) {
for (const inlineJsQuery of inlineJsMatches) {
try {
const code = inlineJsQuery[0];
const markdown = await compiler.inlineDataviewJS(inlineJsQuery[1].trim());
replacedText = replacedText.replace(code, markdown);
} catch (e) {
console.error(e);
return inlineJsQuery[0];
}
}
}
return replacedText;
Expand Down

0 comments on commit e3b4ab5

Please sign in to comment.