Skip to content

Instantly share code, notes, and snippets.

@andrewmcodes
Last active May 22, 2023 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewmcodes/810b0a6482e6c4f638e02a5b4eaa4960 to your computer and use it in GitHub Desktop.
Save andrewmcodes/810b0a6482e6c4f638e02a5b4eaa4960 to your computer and use it in GitHub Desktop.
RubyGems QuickAdd Marcro for Obsidian
const notice = (msg) => new Notice(msg, 5000);
const log = (msg) => console.log(msg);
const RUBYGEMS_API_URL = "https://rubygems.org/api/v1/gems/";
let QuickAdd;
module.exports = async function start(params) {
QuickAdd = params;
let clipBoardContents = await QuickAdd.quickAddApi.utility.getClipboard();
const name = await QuickAdd.quickAddApi.inputPrompt(
"Enter Gem name: ", clipBoardContents, clipBoardContents // clipBoardContents is added once as the prompt text and once as the default value
);
if (!name) {
notice("No gem entered.");
throw new Error("No gem entered.");
}
const finalURL = RUBYGEMS_API_URL + name + ".json";
const response = await fetch(finalURL);
const gemData = await response.json();
if (gemData.error) {
notice("Gem not found.");
throw new Error("Gem not found.");
}
QuickAdd.variables = {
...gemData,
id: gemData.name,
name: gemData.name,
authors: gemData.authors,
description: gemData.info.split("\n")[0].substring(0, 100).replace(/\n/g, "").trim(),
github: gemData.metadata.source_code_uri,
docs: gemData.documentation_uri,
homepage: gemData.homepage_uri,
fileName: replaceIllegalFileNameCharactersInString(gemData.name),
devDependencies: gemData.dependencies.development.map((dep) => `[[${dep.name}]]`).join(", "),
dependencies: gemData.dependencies.runtime.map((dep) => `[[${dep.name}]]`).join(", "),
};
}
function replaceIllegalFileNameCharactersInString(string) {
return string.replace(/[\\,#%&\{\}\/*<>?$\'\":@]*/g, "");
}

Download GemPluginImport.js to wherever you keep your user scripts like _assets/scripts

Open the QuickAdd settings and press the Manage Macros button

Add a new macro named something like ImportGem and press configure

Select the name of your user script and press Add

2023-05-22T1341-Obsidian

Press the Template button and fill it out however you want, using the template provided.

2023-05-22T1341-Obsidian 2023-05-22T1341-Obsidian

Then on the main QuickAdd settings page, add a new Macro choice and select the name of your script

2023-05-22T1342-Obsidian 2023-05-22T1342-Obsidian

PRESS VIEW RAW


type: gem id: {{VALUE:id}} aliases: [] cssClass: cards urls: docs: "{{VALUE:docs}}" github: "{{VALUE:github}}" homepage: "{{VALUE:homepage}}"

[!info]

Name:: {{VALUE:name}} Description:: {{VALUE:description}} Authors:: {{VALUE:authors}} Dependencies:: {{VALUE:dependencies}} DevDependencies:: {{VALUE:devDependencies}}

[!links]

function titleCase(str) {
  return str.charAt(0).toUpperCase() + str.slice(1);
}
dv.list(
  Object.entries(dv.current().urls)
    .filter(([name, url]) => {
      return url && url.length > 0;
    })
    .map(([name, url]) => {
      return dv.paragraph(`[${name == "github" ? "GitHub" : titleCase(name)}](${url})`);
    })
);

[!db] Mentions

TABLE WITHOUT ID
  file.link AS "Name",
  type AS "Type"
FROM [[<% tp.file.title %>]]
SORT file.name ASC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment