Skip to content

Instantly share code, notes, and snippets.

@jeetsukumaran
Created June 8, 2023 16:44
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 jeetsukumaran/a114d59a316f185bbf6a77bc24b200ff to your computer and use it in GitHub Desktop.
Save jeetsukumaran/a114d59a316f185bbf6a77bc24b200ff to your computer and use it in GitHub Desktop.
var utility = require(app.vault.adapter.basePath + "/__annex__/resources/obsidian/scripts/javascript/utility.js")
function formatContributorList(dv, plan) {
let contributors = dv.pages()
.where((p) => p.file.etags.includes(plan.workplanTag))
.where((p) => p.file.path !== plan.file.path)
.sort((p) => p["content-brief"] + p.title + p.file.name)
.map((p) => {
let contributor = [];
let display = p["production-brief"] ?? p["content-brief"] ?? p.title ?? p.file.name;
contributor.push(dv.fileLink(p.file.path, false, display.trim()));
for (let metadataKey of ["content-type", "content-status", "production-lane"]) {
if (p[metadataKey]) {
contributor.push(`(${metadataKey}:: ${p[metadataKey]})`);
}
}
return contributor.join(" ♢ ");
});
return contributors;
}
function formatFileLink(p) {
return "[" + (p["content-brief"] ?? p["production-brief"] ?? p["title"] ?? p.file.name).trim() + "](" + p.file.path + ")"
}
function contributorTable(dv, contributors, options) {
contributors = contributors.sort((p) =>
(p["document-sort-prefix"] ?? "")
+ (p["content-brief"] ?? "")
+ (p["production-brief"] ?? "")
+ (p["title"] ?? "")
+ p.file.name
)
let result = []
result.push("")
const table = dv.markdownTable(
["Contributor", "Type", "Status", "Lane"],
contributors.map((p) => [
formatFileLink(p),
p["content-type"],
p["content-status"],
p["content-lane"],
]))
result.push(... table.split("\n"))
result.push("")
return result
}
function formatContributorSection(dv, production, options) {
let contributors = dv.pages()
.where((p) => p.file.etags.includes(production["production-tag"]))
.where((p) => p.file.path !== production.file.path)
.sort((p) => p["content-brief"] + p.title + p.file.name)
let retiredStatuses = ["unplanned", "archived"]
let currentContributors = contributors.where((p) => !retiredStatuses.includes(p["production-lane"]))
let exContributors = contributors.where((p) => retiredStatuses.includes(p["production-lane"]))
let outputRows = []
if (currentContributors.length > 0) {
outputRows.push(... contributorTable(dv, currentContributors, options))
} else {
outputRows.push("(*No current documents contributing to this production*)")
}
if (exContributors.length > 0) {
outputRows.push("")
outputRows.push(...
utility.wrapRowsInCallout(
contributorTable(dv, exContributors, options),
{
// calloutTitle: production["production-tag"],
calloutTitle: "Historical",
calloutType: options.calloutType ?? "nested",
isCalloutOpen: true,
}
)
)
outputRows.push("")
}
return outputRows
}
function renderProductionRows(dv, production, options) {
let pdesc = []
// pdesc.push("### " + formatFileLink(production))
pdesc.push("")
pdesc.push("- Log: " + production.file.link)
pdesc.push("- Tag: " + production["production-tag"])
pdesc.push("\n")
const contributorTable = formatContributorSection(dv, production, options)
pdesc.push(... contributorTable)
pdesc.push("\n")
pdesc = utility.wrapRowsInCallout(
pdesc,
{
// calloutType: options.calloutType ?? "info",
// calloutTitle: "##### " + formatFileLink(production),
calloutTitle: formatFileLink(production),
calloutType: options.calloutType ?? "nested",
isCalloutOpen: options.isFileCalloutOpen ?? true,
}
)
return pdesc
}
function renderProductionSingle(dv, subgroupLabel, productionSubgroups, options) {
let subRows = utility.formatGroupedData(
productionSubgroups,
function(i, options) { return renderProductionRows(dv, i, options) },
options,
)
if (false) {
outputRows.push("### " + subgroupLabel)
} else if (options.isGroupedInCallouts) {
subRows = utility.wrapRowsInCallout(
subRows,
{
// calloutTitle: "### " + subgroupLabel,
calloutTitle: subgroupLabel,
calloutType: options.calloutType ?? "nested",
isCalloutOpen: options.isCalloutOpen ?? true,
}
)
}
return subRows
}
function renderProductionViewGlobal(dv, options) {
options = options ?? {}
options.calloutType = (options.calloutType ?? "") + " nested"
// options.calloutType = "nested"
let productions = dv.pages("#production-plan")
if (options.tags) {
for (let tag of options.tags) {
productions = productions.where(p => p.file.tags.some(tt => tt.includes(tag)))
}
}
productions = productions.sort((p) => p["production-area"])
.sort((p) => p["production-priority"])
.sort((p) => p["production-brief"])
.sort((p) => p["production-tag"])
productionGroups = {}
for (const production of productions) {
const productionSubgroupKey = production["production-status"]
const productionSupergroupKey = production["production-area"] ?? "unassigned"
if (!productionGroups[productionSupergroupKey]) {
productionGroups[productionSupergroupKey] = {}
}
if (!productionGroups[productionSupergroupKey][productionSubgroupKey]) {
productionGroups[productionSupergroupKey][productionSubgroupKey] = []
}
productionGroups[productionSupergroupKey][productionSubgroupKey].push(production)
}
let outputRows = []
options.isGroupedInCallouts = options.isGroupedInCallouts ?? true
options.isGroupCalloutOpenFn = function(g) { return options.isSubgroupCalloutsOpen ?? g == "active" }
// options.groupTitlePrefix = "#### " + (options.groupTitlePrefix ?? "")
for (let subgroupLabel of Object.keys(productionGroups).sort()) {
const subRows = renderProductionSingle(dv, subgroupLabel, productionGroups[subgroupLabel], options)
outputRows.push(... subRows)
}
dv.span(outputRows.join("\n"))
}
function runQuery(dv, options) {
renderProductionViewGlobal(dv, options)
}
runQuery(dv, input);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment