You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
697 lines
114 KiB
697 lines
114 KiB
3 years ago
|
'use strict';
|
||
|
|
||
|
var obsidian = require('obsidian');
|
||
|
|
||
|
/*! *****************************************************************************
|
||
|
Copyright (c) Microsoft Corporation.
|
||
|
|
||
|
Permission to use, copy, modify, and/or distribute this software for any
|
||
|
purpose with or without fee is hereby granted.
|
||
|
|
||
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||
|
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||
|
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||
|
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||
|
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||
|
PERFORMANCE OF THIS SOFTWARE.
|
||
|
***************************************************************************** */
|
||
|
/* global Reflect, Promise */
|
||
|
|
||
|
var extendStatics = function(d, b) {
|
||
|
extendStatics = Object.setPrototypeOf ||
|
||
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||
|
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
||
|
return extendStatics(d, b);
|
||
|
};
|
||
|
|
||
|
function __extends(d, b) {
|
||
|
if (typeof b !== "function" && b !== null)
|
||
|
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
||
|
extendStatics(d, b);
|
||
|
function __() { this.constructor = d; }
|
||
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||
|
}
|
||
|
|
||
|
var __assign = function() {
|
||
|
__assign = Object.assign || function __assign(t) {
|
||
|
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
||
|
s = arguments[i];
|
||
|
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
||
|
}
|
||
|
return t;
|
||
|
};
|
||
|
return __assign.apply(this, arguments);
|
||
|
};
|
||
|
|
||
|
function __awaiter(thisArg, _arguments, P, generator) {
|
||
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||
|
return new (P || (P = Promise))(function (resolve, reject) {
|
||
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function __generator(thisArg, body) {
|
||
|
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||
|
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
||
|
function step(op) {
|
||
|
if (f) throw new TypeError("Generator is already executing.");
|
||
|
while (_) try {
|
||
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
||
|
switch (op[0]) {
|
||
|
case 0: case 1: t = op; break;
|
||
|
case 4: _.label++; return { value: op[1], done: false };
|
||
|
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||
|
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||
|
default:
|
||
|
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||
|
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||
|
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||
|
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||
|
if (t[2]) _.ops.pop();
|
||
|
_.trys.pop(); continue;
|
||
|
}
|
||
|
op = body.call(thisArg, _);
|
||
|
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function __read(o, n) {
|
||
|
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
||
|
if (!m) return o;
|
||
|
var i = m.call(o), r, ar = [], e;
|
||
|
try {
|
||
|
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
||
|
}
|
||
|
catch (error) { e = { error: error }; }
|
||
|
finally {
|
||
|
try {
|
||
|
if (r && !r.done && (m = i["return"])) m.call(i);
|
||
|
}
|
||
|
finally { if (e) throw e.error; }
|
||
|
}
|
||
|
return ar;
|
||
|
}
|
||
|
|
||
|
function __spreadArray(to, from, pack) {
|
||
|
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
||
|
if (ar || !(i in from)) {
|
||
|
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
||
|
ar[i] = from[i];
|
||
|
}
|
||
|
}
|
||
|
return to.concat(ar || from);
|
||
|
}
|
||
|
|
||
|
function formatContent(content) {
|
||
|
return content.split('\n');
|
||
|
}
|
||
|
function getAllExpandersQuery(content) {
|
||
|
var accum = [];
|
||
|
for (var i = 0; i < content.length; i++) {
|
||
|
var line = content[i];
|
||
|
if (line === '```expander') {
|
||
|
for (var e = 0; e < content.length - i; e++) {
|
||
|
var nextline = content[i + e];
|
||
|
if (nextline === '```') {
|
||
|
accum.push({
|
||
|
start: i,
|
||
|
end: i + e,
|
||
|
query: content[i + 1],
|
||
|
template: e > 2 ? content.slice(i + 2, i + e).join('\n') : ''
|
||
|
});
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return accum;
|
||
|
}
|
||
|
function getClosestQuery(queries, lineNumber) {
|
||
|
if (queries.length === 0) {
|
||
|
return undefined;
|
||
|
}
|
||
|
return queries.reduce(function (a, b) {
|
||
|
return Math.abs(b.start - lineNumber) < Math.abs(a.start - lineNumber) ? b : a;
|
||
|
});
|
||
|
}
|
||
|
function getLastLineToReplace(content, query, endline) {
|
||
|
var lineFrom = query.end;
|
||
|
for (var i = lineFrom + 1; i < content.length; i++) {
|
||
|
if (content[i] === endline) {
|
||
|
return i;
|
||
|
}
|
||
|
}
|
||
|
return lineFrom + 1;
|
||
|
}
|
||
|
function trimContent(s) {
|
||
|
var removeEmptyLines = function (s) {
|
||
|
var lines = s.split('\n').map(function (e) { return e.trim(); });
|
||
|
if (lines.length < 2) {
|
||
|
return s;
|
||
|
}
|
||
|
if (lines.indexOf('') === 0) {
|
||
|
return removeEmptyLines(lines.slice(1).join('\n'));
|
||
|
}
|
||
|
return s;
|
||
|
};
|
||
|
var removeFrontMatter = function (s, lookEnding) {
|
||
|
if (lookEnding === void 0) { lookEnding = false; }
|
||
|
var lines = s.split('\n');
|
||
|
if (lookEnding && lines.indexOf('---') === 0) {
|
||
|
return lines.slice(1).join('\n');
|
||
|
}
|
||
|
if (lookEnding) {
|
||
|
return removeFrontMatter(lines.slice(1).join('\n'), true);
|
||
|
}
|
||
|
if (lines.indexOf('---') === 0) {
|
||
|
return removeFrontMatter(lines.slice(1).join('\n'), true);
|
||
|
}
|
||
|
return s;
|
||
|
};
|
||
|
return removeFrontMatter(removeEmptyLines(s));
|
||
|
}
|
||
|
|
||
|
var TextExpander = /** @class */ (function (_super) {
|
||
|
__extends(TextExpander, _super);
|
||
|
function TextExpander(app, plugin) {
|
||
|
var _this = _super.call(this, app, plugin) || this;
|
||
|
_this.config = {
|
||
|
autoExpand: false,
|
||
|
defaultTemplate: '- $link',
|
||
|
delay: 5000,
|
||
|
excludeCurrent: true,
|
||
|
lineEnding: '<--->'
|
||
|
};
|
||
|
_this.seqs = [
|
||
|
{
|
||
|
name: '\\$count',
|
||
|
loop: true,
|
||
|
format: function (_s, _content, _file, _d, index) { return index ? String(index + 1) : String(1); },
|
||
|
desc: 'add index number to each produced file'
|
||
|
},
|
||
|
{
|
||
|
name: '\\$filename',
|
||
|
loop: true,
|
||
|
format: function (_s, _content, file) { return file.basename; },
|
||
|
desc: 'name of the founded file'
|
||
|
},
|
||
|
{
|
||
|
name: '\\$link',
|
||
|
loop: true,
|
||
|
format: function (_s, _content, file) { return _this.app.fileManager.generateMarkdownLink(file, file.path); },
|
||
|
desc: 'link based on Obsidian settings'
|
||
|
},
|
||
|
{
|
||
|
name: '\\$lines:\\d+',
|
||
|
loop: true,
|
||
|
readContent: true,
|
||
|
format: function (s, content, _file) {
|
||
|
var digits = Number(s.split(':')[1]);
|
||
|
return trimContent(content)
|
||
|
.split('\n')
|
||
|
.filter(function (_, i) { return i < digits; })
|
||
|
.join('\n')
|
||
|
.replace(new RegExp(_this.config.lineEnding, 'g'), '');
|
||
|
},
|
||
|
desc: 'specified count of lines from the found file'
|
||
|
},
|
||
|
{
|
||
|
name: '\\$frontmatter:[\\p\{L\}_-]+',
|
||
|
loop: true,
|
||
|
format: function (s, _content, file) { return _this.getFrontMatter(s, file); },
|
||
|
desc: 'value from the frontmatter key in the found file'
|
||
|
},
|
||
|
{
|
||
|
name: '\\$lines+',
|
||
|
loop: true,
|
||
|
readContent: true,
|
||
|
format: function (s, content, _file) { return content.replace(new RegExp(_this.config.lineEnding, 'g'), ''); },
|
||
|
desc: 'all content from the found file'
|
||
|
},
|
||
|
{
|
||
|
name: '\\$ext',
|
||
|
loop: true,
|
||
|
format: function (s, content, file) { return file.extension; },
|
||
|
desc: 'return file extension'
|
||
|
},
|
||
|
{
|
||
|
name: '\\$created:format:date',
|
||
|
loop: true,
|
||
|
format: function (s, content, file) { return String(new Date(file.stat.ctime).toISOString()).split('T')[0]; },
|
||
|
desc: 'created time formatted'
|
||
|
},
|
||
|
{
|
||
|
name: '\\$created:format:time',
|
||
|
loop: true,
|
||
|
format: function (s, content, file) { return String(new Date(file.stat.ctime).toISOString()).split(/([.T])/)[2]; },
|
||
|
desc: 'created time formatted'
|
||
|
},
|
||
|
{
|
||
|
name: '\\$created:format',
|
||
|
loop: true,
|
||
|
format: function (s, content, file) { return String(new Date(file.stat.ctime).toISOString()); },
|
||
|
desc: 'created time formatted'
|
||
|
},
|
||
|
{
|
||
|
name: '\\$created',
|
||
|
loop: true,
|
||
|
format: function (s, content, file) { return String(file.stat.ctime); },
|
||
|
desc: 'created time'
|
||
|
},
|
||
|
{
|
||
|
name: '\\$size',
|
||
|
loop: true,
|
||
|
format: function (s, content, file) { return String(file.stat.size); },
|
||
|
desc: 'size of the file'
|
||
|
},
|
||
|
{
|
||
|
name: '\\$path',
|
||
|
loop: true,
|
||
|
format: function (s, content, file) { return file.path; },
|
||
|
desc: 'path to the found file'
|
||
|
},
|
||
|
{
|
||
|
name: '\\$parent',
|
||
|
loop: true,
|
||
|
format: function (s, content, file) { return file.parent.name; },
|
||
|
desc: 'parent folder name'
|
||
|
},
|
||
|
{
|
||
|
name: '^(.+|)\\$header:.+',
|
||
|
loop: true,
|
||
|
format: function (s, content, file) {
|
||
|
var _a;
|
||
|
var prefix = s.slice(0, s.indexOf('$'));
|
||
|
var header = s.slice(s.indexOf('$')).replace('$header:', '').replace(/"/g, '');
|
||
|
var neededLevel = header.split("#").length - 1;
|
||
|
var neededTitle = header.replace(/^#+/g, '').trim();
|
||
|
var metadata = _this.app.metadataCache.getFileCache(file);
|
||
|
return ((_a = metadata.headings) === null || _a === void 0 ? void 0 : _a.filter(function (e) {
|
||
|
var tests = [
|
||
|
[neededTitle, e.heading.includes(neededTitle)],
|
||
|
[neededLevel, e.level === neededLevel]
|
||
|
].filter(function (e) { return e[0]; });
|
||
|
if (tests.length) {
|
||
|
return tests.map(function (e) { return e[1]; }).every(function (e) { return e === true; });
|
||
|
}
|
||
|
return true;
|
||
|
}).map(function (h) { return _this.app.fileManager.generateMarkdownLink(file, file.path, '#' + h.heading); }).map(function (link) { return prefix + link; }).join('\n')) || '';
|
||
|
},
|
||
|
desc: 'headings from founded files. $header:## - return all level 2 headings. $header:Title - return all heading which match the string. Can be prepended like: - !$header:## to transclude the headings.'
|
||
|
},
|
||
|
{
|
||
|
name: '^(.+|)\\$blocks',
|
||
|
readContent: true,
|
||
|
loop: true,
|
||
|
format: function (s, content, file) {
|
||
|
return content
|
||
|
.split('\n')
|
||
|
.filter(function (e) { return /\^\w+$/.test(e); })
|
||
|
.map(function (e) { return s
|
||
|
.replace('$blocks', "(" + encodeURIComponent(file.basename) + "#" + e.replace(/^.+?(\^\w+$)/, '$1') + ")"); })
|
||
|
.join('\n');
|
||
|
},
|
||
|
desc: 'block ids from the found files. Can be prepended.'
|
||
|
},
|
||
|
{
|
||
|
name: '^(.+|)\\$match:header', loop: true, format: function (s, content, file, results) {
|
||
|
var _a;
|
||
|
var prefix = s.slice(0, s.indexOf('$'));
|
||
|
var metadata = _this.app.metadataCache.getFileCache(file);
|
||
|
var headings = (_a = metadata.headings) === null || _a === void 0 ? void 0 : _a.filter(function (h) { return results.result.content.filter(function (c) { return h.position.end.offset < c[0]; }).some(function (e) { return e; }); }).slice(-1);
|
||
|
return headings
|
||
|
.map(function (h) { return _this.app.fileManager.generateMarkdownLink(file, file.path, '#' + h.heading); })
|
||
|
.map(function (link) { return prefix + link; })
|
||
|
.join('\n') || '';
|
||
|
}, desc: 'extract found selections'
|
||
|
},
|
||
|
{
|
||
|
name: '^(.+|)\\$match', loop: true, format: function (s, content, file, results) {
|
||
|
var _a;
|
||
|
var prefix = s.slice(0, s.indexOf('$'));
|
||
|
return (_a = results.result.content) === null || _a === void 0 ? void 0 : _a.map(function (t) {
|
||
|
var _a;
|
||
|
return (_a = results.content).slice.apply(_a, __spreadArray([], __read(t)));
|
||
|
}).map(function (t) { return prefix + t; }).join('\n');
|
||
|
}, desc: 'extract found selections'
|
||
|
},
|
||
|
];
|
||
|
_this.search = _this.search.bind(_this);
|
||
|
_this.initExpander = _this.initExpander.bind(_this);
|
||
|
_this.reformatLinks = _this.reformatLinks.bind(_this);
|
||
|
return _this;
|
||
|
}
|
||
|
TextExpander.prototype.getFrontMatter = function (s, r) {
|
||
|
var _a = this.app.metadataCache.getCache(r.path).frontmatter, frontmatter = _a === void 0 ? null : _a;
|
||
|
if (frontmatter) {
|
||
|
return frontmatter[s.split(':')[1]] || '';
|
||
|
}
|
||
|
return '';
|
||
|
};
|
||
|
TextExpander.prototype.reformatLinks = function (links, mapFunc) {
|
||
|
var _a, _b, _c, _e;
|
||
|
if (mapFunc === void 0) { mapFunc = function (s) { return '[[' + s + ']]'; }; }
|
||
|
var currentView = this.app.workspace.activeLeaf.view;
|
||
|
if (currentView instanceof obsidian.FileView) {
|
||
|
return (_b = (_a = links === null || links === void 0 ? void 0 : links.map(function (e) { return e.basename; }).filter(function (e) { return currentView.file.basename !== e; })) === null || _a === void 0 ? void 0 : _a.map(mapFunc)) === null || _b === void 0 ? void 0 : _b.join('\n');
|
||
|
}
|
||
|
return (_e = (_c = links === null || links === void 0 ? void 0 : links.map(function (e) { return e.basename; })) === null || _c === void 0 ? void 0 : _c.map(mapFunc)) === null || _e === void 0 ? void 0 : _e.join('\n');
|
||
|
};
|
||
|
TextExpander.prototype.search = function (s) {
|
||
|
// @ts-ignore
|
||
|
var globalSearchFn = this.app.internalPlugins.getPluginById('global-search').instance.openGlobalSearch.bind(this);
|
||
|
var search = function (query) { return globalSearchFn(query); };
|
||
|
var leftSplitState = {
|
||
|
// @ts-ignore
|
||
|
collapsed: this.app.workspace.leftSplit.collapsed,
|
||
|
// @ts-ignore
|
||
|
tab: this.app.workspace.leftSplit.children[0].currentTab
|
||
|
};
|
||
|
search(s);
|
||
|
if (leftSplitState.collapsed) {
|
||
|
// @ts-ignore
|
||
|
this.app.workspace.leftSplit.collapse();
|
||
|
}
|
||
|
// @ts-ignore
|
||
|
if (leftSplitState.tab !== this.app.workspace.leftSplit.children[0].currentTab) {
|
||
|
// @ts-ignore
|
||
|
this.app.workspace.leftSplit.children[0].selectTabIndex(leftSplitState.tab);
|
||
|
}
|
||
|
};
|
||
|
TextExpander.prototype.getFoundAfterDelay = function () {
|
||
|
return __awaiter(this, void 0, void 0, function () {
|
||
|
var searchLeaf, view;
|
||
|
var _this = this;
|
||
|
return __generator(this, function (_a) {
|
||
|
switch (_a.label) {
|
||
|
case 0:
|
||
|
searchLeaf = this.app.workspace.getLeavesOfType('search')[0];
|
||
|
return [4 /*yield*/, searchLeaf.open(searchLeaf.view)];
|
||
|
case 1:
|
||
|
view = _a.sent();
|
||
|
return [2 /*return*/, new Promise(function (resolve) {
|
||
|
setTimeout(function () {
|
||
|
// @ts-ignore
|
||
|
var results = view.dom.resultDomLookup;
|
||
|
return resolve(results);
|
||
|
}, _this.config.delay);
|
||
|
})];
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
};
|
||
|
TextExpander.prototype.startTemplateMode = function (query, lastLine) {
|
||
|
var _a;
|
||
|
return __awaiter(this, void 0, void 0, function () {
|
||
|
var currentView, currentFileName, templateContent, heading, footer, repeatableContent, searchResults, files, filterFiles, format, changed, result, viewBeforeReplace;
|
||
|
var _this = this;
|
||
|
return __generator(this, function (_b) {
|
||
|
switch (_b.label) {
|
||
|
case 0:
|
||
|
currentView = this.app.workspace.activeLeaf.view;
|
||
|
currentFileName = '';
|
||
|
templateContent = query.template.split('\n');
|
||
|
heading = templateContent.filter(function (e) { return e[0] === '^'; }).map(function (s) { return s.slice(1); });
|
||
|
footer = templateContent.filter(function (e) { return e[0] === '>'; }).map(function (s) { return s.slice(1); });
|
||
|
repeatableContent = templateContent.filter(function (e) { return e[0] !== '^' && e[0] !== '>'; }).filter(function (e) { return e; }).length === 0
|
||
|
? [this.config.defaultTemplate]
|
||
|
: templateContent.filter(function (e) { return e[0] !== '^' && e[0] !== '>'; }).filter(function (e) { return e; });
|
||
|
if (currentView instanceof obsidian.FileView) {
|
||
|
currentFileName = currentView.file.basename;
|
||
|
}
|
||
|
return [4 /*yield*/, this.getFoundAfterDelay()];
|
||
|
case 1:
|
||
|
searchResults = _b.sent();
|
||
|
files = Array.from(searchResults.keys());
|
||
|
filterFiles = this.config.excludeCurrent
|
||
|
? files.filter(function (file) { return file.basename !== currentFileName; })
|
||
|
: files;
|
||
|
format = function (r, template, index) { return __awaiter(_this, void 0, void 0, function () {
|
||
|
var fileContent, _a;
|
||
|
return __generator(this, function (_b) {
|
||
|
switch (_b.label) {
|
||
|
case 0:
|
||
|
if (!(new RegExp(this.seqs.filter(function (e) { return e.readContent; }).map(function (e) { return e.name; }).join('|')).test(template))) return [3 /*break*/, 2];
|
||
|
return [4 /*yield*/, this.app.vault.cachedRead(r)];
|
||
|
case 1:
|
||
|
_a = _b.sent();
|
||
|
return [3 /*break*/, 3];
|
||
|
case 2:
|
||
|
_a = '';
|
||
|
_b.label = 3;
|
||
|
case 3:
|
||
|
fileContent = _a;
|
||
|
return [2 /*return*/, this.seqs.reduce(function (acc, seq) {
|
||
|
return acc.replace(new RegExp(seq.name, 'gu'), function (replace) { return seq.format(replace, fileContent, r, searchResults.get(r), index); });
|
||
|
}, template)];
|
||
|
}
|
||
|
});
|
||
|
}); };
|
||
|
return [4 /*yield*/, Promise.all(filterFiles
|
||
|
.map(function (file, i) { return __awaiter(_this, void 0, void 0, function () {
|
||
|
var result;
|
||
|
var _this = this;
|
||
|
return __generator(this, function (_a) {
|
||
|
switch (_a.label) {
|
||
|
case 0: return [4 /*yield*/, Promise.all(repeatableContent.map(function (s) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
||
|
switch (_a.label) {
|
||
|
case 0: return [4 /*yield*/, format(file, s, i)];
|
||
|
case 1: return [2 /*return*/, _a.sent()];
|
||
|
}
|
||
|
}); }); }))];
|
||
|
case 1:
|
||
|
result = _a.sent();
|
||
|
return [2 /*return*/, result.join('\n')];
|
||
|
}
|
||
|
});
|
||
|
}); }))];
|
||
|
case 2:
|
||
|
changed = _b.sent();
|
||
|
result = [
|
||
|
' ',
|
||
|
heading.join('\n'),
|
||
|
changed.join('\n'),
|
||
|
footer.join('\n'),
|
||
|
' ',
|
||
|
this.config.lineEnding
|
||
|
].filter(function (e) { return e; }).join('\n');
|
||
|
viewBeforeReplace = this.app.workspace.activeLeaf.view;
|
||
|
if (viewBeforeReplace instanceof obsidian.MarkdownView) {
|
||
|
if (viewBeforeReplace.file.basename !== currentFileName) {
|
||
|
return [2 /*return*/];
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
return [2 /*return*/];
|
||
|
}
|
||
|
this.cm.replaceRange(result, { line: query.end + 1, ch: 0 }, { line: lastLine, ch: ((_a = this.cm.getLine(lastLine)) === null || _a === void 0 ? void 0 : _a.length) || 0 });
|
||
|
return [2 /*return*/, Promise.resolve()];
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
};
|
||
|
TextExpander.prototype.runQuery = function (query, content) {
|
||
|
var _a;
|
||
|
return __awaiter(this, void 0, void 0, function () {
|
||
|
var lastLine, newContent;
|
||
|
return __generator(this, function (_b) {
|
||
|
switch (_b.label) {
|
||
|
case 0:
|
||
|
if (!query) {
|
||
|
new Notification('Expand query not found');
|
||
|
return [2 /*return*/, Promise.resolve()];
|
||
|
}
|
||
|
lastLine = getLastLineToReplace(content, query, this.config.lineEnding);
|
||
|
this.cm.replaceRange(this.config.lineEnding, { line: query.end + 1, ch: 0 }, { line: lastLine, ch: ((_a = this.cm.getLine(lastLine)) === null || _a === void 0 ? void 0 : _a.length) || 0 });
|
||
|
newContent = formatContent(this.cm.getValue());
|
||
|
this.search(query.query);
|
||
|
return [4 /*yield*/, this.startTemplateMode(query, getLastLineToReplace(newContent, query, this.config.lineEnding))];
|
||
|
case 1: return [2 /*return*/, _b.sent()];
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
};
|
||
|
TextExpander.prototype.initExpander = function (all) {
|
||
|
var _this = this;
|
||
|
if (all === void 0) { all = false; }
|
||
|
var currentView = this.app.workspace.activeLeaf.view;
|
||
|
if (!(currentView instanceof obsidian.MarkdownView)) {
|
||
|
return;
|
||
|
}
|
||
|
var cmDoc = this.cm = currentView.sourceMode.cmEditor;
|
||
|
var curNum = cmDoc.getCursor().line;
|
||
|
var content = cmDoc.getValue();
|
||
|
var formatted = formatContent(content);
|
||
|
var findQueries = getAllExpandersQuery(formatted);
|
||
|
var closestQuery = getClosestQuery(findQueries, curNum);
|
||
|
if (all) {
|
||
|
findQueries.reduce(function (promise, query, i) {
|
||
|
return promise.then(function () {
|
||
|
var newContent = formatContent(cmDoc.getValue());
|
||
|
var updatedQueries = getAllExpandersQuery(newContent);
|
||
|
return _this.runQuery(updatedQueries[i], newContent);
|
||
|
});
|
||
|
}, Promise.resolve());
|
||
|
}
|
||
|
else {
|
||
|
this.runQuery(closestQuery, formatted);
|
||
|
}
|
||
|
};
|
||
|
TextExpander.prototype.onload = function () {
|
||
|
return __awaiter(this, void 0, void 0, function () {
|
||
|
var data;
|
||
|
var _this = this;
|
||
|
return __generator(this, function (_a) {
|
||
|
switch (_a.label) {
|
||
|
case 0:
|
||
|
this.addSettingTab(new SettingTab(this.app, this));
|
||
|
this.addCommand({
|
||
|
id: 'editor-expand',
|
||
|
name: 'expand',
|
||
|
callback: this.initExpander,
|
||
|
hotkeys: []
|
||
|
});
|
||
|
this.addCommand({
|
||
|
id: 'editor-expand-all',
|
||
|
name: 'expand all',
|
||
|
callback: function () { return _this.initExpander(true); },
|
||
|
hotkeys: []
|
||
|
});
|
||
|
this.app.workspace.on('file-open', function () { return __awaiter(_this, void 0, void 0, function () {
|
||
|
var activeLeaf, activeView, isAllowedView;
|
||
|
return __generator(this, function (_a) {
|
||
|
if (!this.config.autoExpand) {
|
||
|
return [2 /*return*/];
|
||
|
}
|
||
|
activeLeaf = this.app.workspace.activeLeaf;
|
||
|
if (!activeLeaf) {
|
||
|
return [2 /*return*/];
|
||
|
}
|
||
|
activeView = activeLeaf.view;
|
||
|
isAllowedView = activeView instanceof obsidian.MarkdownView;
|
||
|
if (!isAllowedView) {
|
||
|
return [2 /*return*/];
|
||
|
}
|
||
|
this.initExpander(true);
|
||
|
return [2 /*return*/];
|
||
|
});
|
||
|
}); });
|
||
|
return [4 /*yield*/, this.loadData()];
|
||
|
case 1:
|
||
|
data = _a.sent();
|
||
|
if (data) {
|
||
|
this.config = __assign(__assign({}, this.config), data);
|
||
|
}
|
||
|
return [2 /*return*/];
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
};
|
||
|
TextExpander.prototype.onunload = function () {
|
||
|
console.log('unloading plugin');
|
||
|
};
|
||
|
TextExpander.prototype.saveSettings = function () {
|
||
|
this.saveData(this.config);
|
||
|
};
|
||
|
return TextExpander;
|
||
|
}(obsidian.Plugin));
|
||
|
var SettingTab = /** @class */ (function (_super) {
|
||
|
__extends(SettingTab, _super);
|
||
|
function SettingTab(app, plugin) {
|
||
|
var _this = _super.call(this, app, plugin) || this;
|
||
|
_this.app = app;
|
||
|
_this.plugin = plugin;
|
||
|
return _this;
|
||
|
}
|
||
|
SettingTab.prototype.display = function () {
|
||
|
var _this = this;
|
||
|
var containerEl = this.containerEl;
|
||
|
containerEl.empty();
|
||
|
containerEl.createEl('h2', { text: 'Settings for Text Expander' });
|
||
|
new obsidian.Setting(containerEl)
|
||
|
.setName('Auto Expand')
|
||
|
.setDesc('Expand all queries in a file once you open it')
|
||
|
.addToggle(function (toggle) {
|
||
|
toggle
|
||
|
.setValue(_this.plugin.config.autoExpand)
|
||
|
.onChange(function (value) {
|
||
|
_this.plugin.config.autoExpand = value;
|
||
|
_this.plugin.saveSettings();
|
||
|
});
|
||
|
});
|
||
|
new obsidian.Setting(containerEl)
|
||
|
.setName('Delay')
|
||
|
.setDesc('Text expander don\' wait until search completed. It waits for a delay and paste result after that.')
|
||
|
.addSlider(function (slider) {
|
||
|
slider.setLimits(1000, 10000, 1000);
|
||
|
slider.setValue(_this.plugin.config.delay);
|
||
|
slider.onChange(function (value) {
|
||
|
_this.plugin.config.delay = value;
|
||
|
_this.plugin.saveSettings();
|
||
|
});
|
||
|
slider.setDynamicTooltip();
|
||
|
});
|
||
|
new obsidian.Setting(containerEl)
|
||
|
.setName('Line ending')
|
||
|
.setDesc('You can specify the text which will appear at the bottom of the generated text.')
|
||
|
.addText(function (text) {
|
||
|
text.setValue(_this.plugin.config.lineEnding)
|
||
|
.onChange(function (val) {
|
||
|
_this.plugin.config.lineEnding = val;
|
||
|
_this.plugin.saveSettings();
|
||
|
});
|
||
|
});
|
||
|
new obsidian.Setting(containerEl)
|
||
|
.setName('Default template')
|
||
|
.setDesc('You can specify default template')
|
||
|
.addText(function (text) {
|
||
|
text.setValue(_this.plugin.config.defaultTemplate)
|
||
|
.onChange(function (val) {
|
||
|
_this.plugin.config.defaultTemplate = val;
|
||
|
_this.plugin.saveSettings();
|
||
|
});
|
||
|
});
|
||
|
new obsidian.Setting(containerEl)
|
||
|
.setName('Exclude current file')
|
||
|
.setDesc('You can specify should text expander exclude results from current file or not')
|
||
|
.addToggle(function (toggle) {
|
||
|
toggle
|
||
|
.setValue(_this.plugin.config.excludeCurrent)
|
||
|
.onChange(function (value) {
|
||
|
_this.plugin.config.excludeCurrent = value;
|
||
|
_this.plugin.saveSettings();
|
||
|
});
|
||
|
});
|
||
|
new obsidian.Setting(containerEl)
|
||
|
.setName('Sequences')
|
||
|
.setDesc('REGEXP - DESCRIPTION')
|
||
|
.setDesc((function () {
|
||
|
var fragment = new DocumentFragment();
|
||
|
var div = fragment.createEl('div');
|
||
|
_this.plugin.seqs
|
||
|
.map(function (e) { return e.name + ' - ' + (e.desc || ''); })
|
||
|
.map(function (e) {
|
||
|
var el = fragment.createEl('div');
|
||
|
el.setText(e);
|
||
|
el.setAttribute('style', "\n border-bottom: 1px solid rgba(255, 255, 255, 0.1);\n margin-bottom: 0.5rem;\n padding-bottom: 0.5rem;\n ");
|
||
|
return el;
|
||
|
}).forEach(function (el) {
|
||
|
div.appendChild(el);
|
||
|
});
|
||
|
fragment.appendChild(div);
|
||
|
return fragment;
|
||
|
})());
|
||
|
};
|
||
|
return SettingTab;
|
||
|
}(obsidian.PluginSettingTab));
|
||
|
|
||
|
module.exports = TextExpander;
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFpbi5qcyIsInNvdXJjZXMiOlsibm9kZV9tb2R1bGVzL3RzbGliL3RzbGliLmVzNi5qcyIsImhlbHBlcnMudHMiLCJtYWluLnRzIl0sInNvdXJjZXNDb250ZW50IjpbIi8qISAqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKlxyXG5Db3B5cmlnaHQgKGMpIE1pY3Jvc29mdCBDb3Jwb3JhdGlvbi5cclxuXHJcblBlcm1pc3Npb24gdG8gdXNlLCBjb3B5LCBtb2RpZnksIGFuZC9vciBkaXN0cmlidXRlIHRoaXMgc29mdHdhcmUgZm9yIGFueVxyXG5wdXJwb3NlIHdpdGggb3Igd2l0aG91dCBmZWUgaXMgaGVyZWJ5IGdyYW50ZWQuXHJcblxyXG5USEUgU09GVFdBUkUgSVMgUFJPVklERUQgXCJBUyBJU1wiIEFORCBUSEUgQVVUSE9SIERJU0NMQUlNUyBBTEwgV0FSUkFOVElFUyBXSVRIXHJcblJFR0FSRCBUTyBUSElTIFNPRlRXQVJFIElOQ0xVRElORyBBTEwgSU1QTElFRCBXQVJSQU5USUVTIE9GIE1FUkNIQU5UQUJJTElUWVxyXG5BTkQgRklUTkVTUy4gSU4gTk8gRVZFTlQgU0hBTEwgVEhFIEFVVEhPUiBCRSBMSUFCTEUgRk9SIEFOWSBTUEVDSUFMLCBESVJFQ1QsXHJcbklORElSRUNULCBPUiBDT05TRVFVRU5USUFMIERBTUFHRV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