OO-PG0001
Plug-Ins: Transform Links
A pair of plug-ins for transforming links in the topic and note of the selected outline rows. If no rows are selected, all rows will be processed.
- Replace Markdown Links • This plug-in will replace all occurrences of links in markdown format [Link-Title](Link-URL-String) with link objects.
- Replace Links with Markdown • This plug-in converts all occurrences of link objects into text in markdown link format: [Link-Title](Link-URL-String)
IMPORTANT: Do no run the “Replace Links with Markdown” plug-in if the note currently contains markdown links. Instead, first convert the markdown links to link objects, then run the script.
Return to: OmniOutliner Plug-In Collection
Video: Adding Links to an Outline |
Demonstrates the use of the transform plug-ins to simplify the process of adding links to an outline document. |
|
Replace Links wth Markdown
/*{
"type": "action",
"targets": ["omnioutliner"],
"author": "Otto Automator",
"identifier": "com.omni-automation.oo.replace-links-with-markdown",
"version": "1.1",
"description": "Converts links objects into links in markdown format, in the selected rows. If no rows are selected, all rows are processed",
"label": "Replace Links with Markdown",
"shortLabel": "Replace Links w/ MD",
"paletteLabel": "Replace Links w/ MD",
"image": "link.circle"
}*/
(() => {
const action = new PlugIn.Action(async function(selection, sender){
function processRow(textObject){
runRanges = textObject.ranges(TextComponent.AttributeRuns)
runRanges.reverse().forEach(range => {
runStyle = textObject.styleForRange(range)
linkStr = runStyle.get(Style.Attribute.Link).string
if(linkStr.length > 0){
linkTitle = textObject.textInRange(range).string
if(linkTitle === linkStr){linkTitle = "LINK"}
mdLinkStr = `[${linkTitle}](${linkStr})`
linkObj = new Text(mdLinkStr, textObject.style)
textObject.replace(range, linkObj)
}
})
}
try {
try {document.name} catch(err){
throw{name:"Missing Resource", message:"No document is open."}
}
editor = document.editors[0]
var rows = editor.selection.items
if(rows.length === 0){
var rows = rootItem.descendants
}
rows.forEach(item => {
textObject = item.valueForColumn(outlineColumn)
if(textObject && !textObject.range.isEmpty){
processRow(textObject)
}
textObject = item.valueForColumn(noteColumn)
if(textObject && !textObject.range.isEmpty){
processRow(textObject)
}
})
}
catch(err){
console.error(err.name, err.message)
new Alert(err.name, err.message).show()
}
});
action.validate = function(selection, sender){
// validation code
// selection options: columns, document, editor, items, nodes, outline, styles
return true
};
return action;
})();
Replace Markdown Links
/*{
"type": "action",
"targets": ["omnioutliner"],
"author": "Otto Automator",
"identifier": "com.omni-automation.oo.replace-markdown-links",
"version": "1.1",
"description": "Converts links in markdown format into styled link text objects, in the selected rows. If no rows are selected, all rows are processed",
"label": "Replace Markdown Links",
"shortLabel": "Replace MD Links",
"paletteLabel": "Replace MD Links",
"image": "link.circle.fill"
}*/
(() => {
const action = new PlugIn.Action(async function(selection, sender){
function processRow(textObject){
var regexMdLinks = /!?\[([^\]]*)\]\(([^\)]+)\)/
var strToMatch = regexMdLinks.source
var findParams = [Text.FindOption.RegularExpression]
var rangeToSearch = textObject.range
var resultRange = textObject.range
while (resultRange !== null) {
var resultRange = textObject.find(regexMdLinks.source, findParams, rangeToSearch)
if (resultRange){
mdLinkStr = textObject.textInRange(resultRange).string
x = mdLinkStr.indexOf("]")
y = mdLinkStr.indexOf("(")
mdLinkTitle = mdLinkStr.substr(1, x - 1)
mdLinkURLStr = mdLinkStr.substr(y + 1, mdLinkStr.length)
mdLinkURLStr = mdLinkURLStr.slice(0, -1)
linkURL = URL.fromString(mdLinkURLStr)
if (linkURL){
linkObj = new Text(mdLinkTitle, textObject.styleForRange(resultRange))
style = linkObj.styleForRange(linkObj.range)
style.set(Style.Attribute.Link, linkURL)
textObject.replace(resultRange, linkObj)
}
if (resultRange.end >= textObject.range.end){
rangeToSearch = null
} else {
// adjust search range to begin from end of last matched range
rangeToSearch = new Text.Range(resultRange.end, textObject.range.end)
}
}
}
}
try {
try {document.name} catch(err){
throw{name:"Missing Resource", message:"No document is open."}
}
editor = document.editors[0]
var rows = editor.selection.items
if(rows.length === 0){
var rows = rootItem.descendants
}
rows.forEach(item => {
textObject = item.valueForColumn(outlineColumn)
if(textObject && !textObject.range.isEmpty){
processRow(textObject)
}
textObject = item.valueForColumn(noteColumn)
if(textObject && !textObject.range.isEmpty){
processRow(textObject)
}
})
}
catch(err){
console.error(err.name, err.message)
new Alert(err.name, err.message).show()
}
});
action.validate = function(selection, sender){
// validation code
// selection options: columns, document, editor, items, nodes, outline, styles
return true
};
return action;
})();