unified#Processor TypeScript Examples
The following examples show how to use
unified#Processor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.ts From dendron with GNU Affero General Public License v3.0 | 7 votes |
static getEngineFromProc(proc: Unified.Processor) {
const engine = proc.data("engine") as DEngineClient;
let error: DendronError | undefined;
if (_.isUndefined(engine) || _.isNull(engine)) {
error = new DendronError({ message: "engine not defined" });
}
return {
error,
engine,
};
}
Example #2
Source File: utils.ts From dendron with GNU Affero General Public License v3.0 | 7 votes |
/**
* Get the vault name, either from processor or passed in vaultName
* @param opts.vaultMissingBehavior how to respond if no vault is found. See {@link VaultMissingBehavior}
*/
static getVault(
proc: Processor,
vaultName?: string,
opts?: { vaultMissingBehavior?: VaultMissingBehavior }
) {
const copts = _.defaults(opts || {}, {
vaultMissingBehavior: VaultMissingBehavior.THROW_ERROR,
});
let { vault } = MDUtilsV4.getDendronData(proc);
const { engine } = MDUtilsV4.getEngineFromProc(proc);
if (vaultName) {
try {
vault = VaultUtils.getVaultByNameOrThrow({
vaults: engine.vaults,
vname: vaultName,
});
} catch (err) {
if (copts.vaultMissingBehavior === VaultMissingBehavior.THROW_ERROR) {
throw err;
}
}
}
return vault;
}
Example #3
Source File: utilsv5.ts From dendron with GNU Affero General Public License v3.0 | 7 votes |
static getProcData(proc: Processor): ProcDataFullV5 {
let _data = proc.data("dendronProcDatav5") as ProcDataFullV5;
// backwards compatibility
_data = _.defaults(MDUtilsV4.getDendronData(proc), _data);
try {
_data.noteRefLvl = MDUtilsV4.getNoteRefLvl(proc);
} catch {
_data.noteRefLvl = 0;
}
return _data || {};
}
Example #4
Source File: utils.ts From dendron with GNU Affero General Public License v3.0 | 7 votes |
static h1ToH2(note: NoteProps, changes: NoteChangeEntry[]) {
const prevNote = { ...note };
return function (this: Processor) {
return (tree: Node, _vfile: VFile) => {
const root = tree as Root;
const idx = _.findIndex(
root.children,
(ent) => ent.type === DendronASTTypes.HEADING && ent.depth === 1
);
if (idx >= 0) {
const head = root.children[idx] as Heading;
head.depth = 2;
changes.push({
note,
prevNote,
status: "update",
});
}
};
};
}
Example #5
Source File: utils.ts From dendron with GNU Affero General Public License v3.0 | 7 votes |
static h1ToTitle(note: NoteProps, changes: NoteChangeEntry[]) {
const prevNote = { ...note };
return function (this: Processor) {
return (tree: Node, _vfile: VFile) => {
const root = tree as Root;
const idx = _.findIndex(
root.children,
(ent) => ent.type === DendronASTTypes.HEADING && ent.depth === 1
);
if (idx >= 0) {
const head = root.children.splice(idx, 1)[0] as Heading;
if (head.children.length === 1 && head.children[0].type === "text") {
note.title = head.children[0].value;
}
changes.push({
note,
prevNote,
status: "update",
});
}
};
};
}
Example #6
Source File: utils.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
/**
* Just parse markdown
*/
static procRemark(opts: { proc?: Processor }) {
const { proc } = opts;
let _proc = proc || this.remark();
return _proc.use(remarkParse, { gfm: true }).use(remarkStringify);
}
Example #7
Source File: utils.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
static getNoteRefLvl(proc: Unified.Processor): number {
return this.getProcOpts(proc).noteRefLvl || 0;
}
Example #8
Source File: utils.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
static getProcOpts(proc: Unified.Processor) {
const procOpts = proc.data(DendronProcDataKeys.PROC_OPTS) as ProcOptsFull;
return procOpts;
}
Example #9
Source File: utils.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
static setEngine(proc: Unified.Processor, engine: DEngineClient) {
proc.data(DendronProcDataKeys.ENGINE, engine);
}
Example #10
Source File: utils.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
static setNoteRefLvl(proc: Unified.Processor, lvl: number) {
this.setProcOpts(proc, { noteRefLvl: lvl });
}
Example #11
Source File: utils.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
static setProcOpts(proc: Unified.Processor, data: Partial<ProcOptsFull>) {
const procOpts = proc.data(DendronProcDataKeys.PROC_OPTS) as ProcOptsFull;
return proc.data(DendronProcDataKeys.PROC_OPTS, { ...procOpts, ...data });
}
Example #12
Source File: utils.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
static getFM(proc: Processor) {
return proc.data("fm") as any;
}
Example #13
Source File: utils.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
/**
* markdown -> html
*/
static procRehype(opts: {
proc?: Processor;
mdPlugins?: Processor[];
mathjax?: boolean;
useLinks?: boolean;
}) {
const { proc, mdPlugins, useLinks } = _.defaults(opts, {
mdPlugins: [],
useLinks: true,
});
let _proc = proc || unified().use(remarkParse, { gfm: true });
mdPlugins.forEach((p) => {
_proc = _proc.use(p);
});
_proc = _proc
.use(remark2rehype, { allowDangerousHtml: true })
.use(rehypePrism, { ignoreMissing: true })
.use(raw)
.use(slug);
if (useLinks) {
_proc = _proc.use(link, {
properties: {
"aria-hidden": "true",
class: "anchor-heading",
},
content: {
type: "element",
// @ts-ignore
tagName: "svg",
properties: {
"aria-hidden": "true",
viewBox: "0 0 16 16",
},
children: [
{
type: "element",
tagName: "use",
properties: {
"xlink:href": "#svg-link",
},
},
],
},
});
}
if (opts.mathjax) {
_proc = _proc.use(katex);
}
return _proc.use(rehypeStringify);
}
Example #14
Source File: utilsv5.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
static getProcOpts(proc: Processor): ProcOptsV5 {
const _data = proc.data("dendronProcOptsv5") as ProcOptsV5;
return _data || {};
}
Example #15
Source File: utilsv5.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
static setNoteRefLvl(proc: Processor, lvl: number) {
// backwards compatibility
MDUtilsV4.setNoteRefLvl(proc, lvl);
return this.setProcData(proc, { noteRefLvl: lvl });
}
Example #16
Source File: utilsv5.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
static setProcData(proc: Processor, opts: Partial<ProcDataFullV5>) {
const _data = proc.data("dendronProcDatav5") as ProcDataFullV5;
// TODO: for backwards compatibility
MDUtilsV4.setProcOpts(proc, opts);
MDUtilsV4.setDendronData(proc, opts);
const notes = _.isUndefined(opts.notes) ? opts?.engine?.notes : opts.notes;
return proc.data("dendronProcDatav5", { ..._data, ...opts, notes });
}
Example #17
Source File: utilsv5.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
static setProcOpts(proc: Processor, opts: ProcOptsV5) {
const _data = proc.data("dendronProcOptsv5") as ProcOptsV5;
return proc.data("dendronProcOptsv5", { ..._data, ...opts });
}
Example #18
Source File: utilsv5.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
static isV5Active(proc: Processor) {
return !_.isUndefined(this.getProcOpts(proc).mode);
}
Example #19
Source File: utilsv5.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
static shouldApplyPublishingRules(proc: Processor): boolean {
return (
this.getProcData(proc).dest === DendronASTDest.HTML &&
this.getProcOpts(proc).flavor === ProcFlavor.PUBLISHING
);
}
Example #20
Source File: utils.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
static setDendronData(proc: Processor, data: Partial<DendronASTData>) {
const _data = proc.data("dendron") as DendronASTData;
return proc.data("dendron", { ..._data, ...data });
}
Example #21
Source File: utils.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
static getDendronData(proc: Processor) {
return proc.data("dendron") as DendronASTData;
}
Example #22
Source File: utils.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
export function addError(proc: Processor, err: DendronError) {
const errors = proc.data("errors") as DendronError[];
errors.push(err);
// no need to put errors back into proc, it's a mutable array
}
Example #23
Source File: utils.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
static convertWikiLinkToNoteUrl(
note: NoteProps,
changes: NoteChangeEntry[],
engine: DEngineClient,
dendronConfig: IntermediateDendronConfig
) {
const prevNote = { ...note };
// eslint-disable-next-line func-names
return function (this: Processor) {
return (tree: Node, _vfile: VFile) => {
const root = tree as DendronASTRoot;
const wikiLinks: WikiLinkNoteV4[] = selectAll(
DendronASTTypes.WIKI_LINK,
root
) as WikiLinkNoteV4[];
let dirty = false;
wikiLinks.forEach((linkNode) => {
let vault: DVault | undefined;
if (!_.isUndefined(linkNode.data.vaultName)) {
vault = VaultUtils.getVaultByName({
vaults: engine.vaults,
vname: linkNode.data.vaultName,
});
}
const existingNote = NoteUtils.getNoteFromMultiVault({
fname: linkNode.value,
engine,
fromVault: note.vault,
toVault: vault,
wsRoot: engine.wsRoot,
});
if (existingNote) {
const publishingConfig =
ConfigUtils.getPublishingConfig(dendronConfig);
const urlRoot = publishingConfig.siteUrl || "";
const { vault } = existingNote;
linkNode.value = WorkspaceUtils.getNoteUrl({
config: dendronConfig,
note: existingNote,
vault,
urlRoot,
anchor: linkNode.data.anchorHeader,
});
dirty = true;
}
});
if (dirty) {
changes.push({
note,
prevNote,
status: "update",
});
}
};
};
}
Example #24
Source File: utils.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
// --- conversion
static convertLinksFromDotNotation(
note: NoteProps,
changes: NoteChangeEntry[]
) {
const prevNote = { ...note };
return function (this: Processor) {
return (tree: Node, _vfile: VFile) => {
const root = tree as DendronASTRoot;
const wikiLinks: WikiLinkNoteV4[] = selectAll(
DendronASTTypes.WIKI_LINK,
root
) as WikiLinkNoteV4[];
let dirty = false;
wikiLinks.forEach((linkNode) => {
let newValue = linkNode.value;
// Add a leading slash to the path as some markdown parsers require it for links
if (!newValue.startsWith("/")) {
newValue = "/" + newValue;
dirty = true;
}
if (linkNode.value.indexOf(".") >= 0) {
newValue = _.replace(newValue, /\./g, "/");
if (linkNode.data.alias === linkNode.value) {
linkNode.data.alias = newValue;
}
dirty = true;
}
// NOTE: important to add this at the end so that we don't convert `.md` to `/md`
linkNode.value = newValue + ".md";
});
//TODO: Add support for Ref Notes and Block Links
if (dirty) {
changes.push({
note,
prevNote,
status: "update",
});
}
};
};
}
Example #25
Source File: prose2mdast.ts From noteworthy with GNU Affero General Public License v3.0 | 6 votes |
makeSerializer = (
prose2mdast: ProseMapper<string>,
mdProcessor: Processor
): MdSerializer => {
// make parser
return (doc: ProseNode): string => {
// Step 1: Convert ProseMirror -> Mdast
let result = proseTreeMap(doc, prose2mdast);
if(result.length > 1) { throw new Error("multiple top-level nodes"); }
if(result.length == 0) { throw new Error("empty document"); }
let ast: Uni.Node = result[0];
console.log("\n-----------\nPROSE2MDAST\n-----------\n");
console.log(JSON.stringify(ast, undefined, 2));
console.log("\n-----------\n");
// Step 2: Use Remark to convert Mdast -> Markdown
return mdProcessor.stringify(ast);
};
}
Example #26
Source File: editor-config.ts From noteworthy with GNU Affero General Public License v3.0 | 5 votes |
private _mdastParser: Processor;
Example #27
Source File: noteRefsV2.ts From dendron with GNU Affero General Public License v3.0 | 5 votes |
plugin: Plugin = function (this: Unified.Processor, opts?: PluginOpts) {
const procOptsV5 = MDUtilsV5.getProcOpts(this);
attachParser(this);
if (this.Compiler != null && !procOptsV5.parseOnly) {
attachCompiler(this, opts);
}
}
Example #28
Source File: noteRefsV2.ts From dendron with GNU Affero General Public License v3.0 | 5 votes |
function attachCompiler(proc: Unified.Processor, opts?: CompilerOpts) {
const Compiler = proc.Compiler;
const visitors = Compiler.prototype.visitors;
const copts = _.defaults(opts || {}, {});
const { dest } = MDUtilsV5.getProcData(proc);
if (visitors) {
visitors.refLinkV2 = function refLinkV2(node: NoteRefNoteV4) {
const ndata = node.data;
if (dest === DendronASTDest.MD_DENDRON) {
const { fname, alias } = ndata.link.from;
const { anchorStart, anchorStartOffset, anchorEnd } = ndata.link.data;
const link = alias ? `${alias}|${fname}` : fname;
let suffix = "";
const vaultPrefix = ndata.link.data.vaultName
? `${CONSTANTS.DENDRON_DELIMETER}${ndata.link.data.vaultName}/`
: "";
if (anchorStart) {
suffix += `#${anchorStart}`;
}
if (anchorStartOffset) {
suffix += `,${anchorStartOffset}`;
}
if (anchorEnd) {
suffix += `:#${anchorEnd}`;
}
return `![[${vaultPrefix}${link}${suffix}]]`;
}
const { error, data } = convertNoteRef({
link: ndata.link,
proc,
compilerOpts: copts,
});
if (error) {
return `ERROR converting ref: ${error.message}`;
}
return data;
};
}
}
Example #29
Source File: noteRefsV2.ts From dendron with GNU Affero General Public License v3.0 | 5 votes |
function attachParser(proc: Unified.Processor) {
function locator(value: string, fromIndex: number) {
return value.indexOf("![[", fromIndex);
}
function inlineTokenizer(eat: Eat, value: string) {
const match = LINK_REGEX.exec(value);
if (match) {
const linkMatch = match[1].trim();
const link = LinkUtils.parseNoteRef(linkMatch);
// If the link is same file [[#header]], it's implicitly to the same file it's located in
if (link.from.fname === "")
link.from.fname = MDUtilsV5.getProcData(proc).fname;
const { value } = LinkUtils.parseLink(linkMatch);
const refNote: NoteRefNoteV4 = {
type: DendronASTTypes.REF_LINK_V2,
data: {
link,
},
value,
};
return eat(match[0])(refNote);
}
return;
}
function inlineTokenizerV5(eat: Eat, value: string) {
const procOpts = MDUtilsV5.getProcOpts(proc);
const match = LINK_REGEX.exec(value);
if (match) {
const linkMatch = match[1].trim();
if (procOpts?.mode === ProcMode.NO_DATA) {
const link = LinkUtils.parseNoteRefRaw(linkMatch);
const { value } = LinkUtils.parseLink(linkMatch);
const refNote: NoteRefNoteRawV4 = {
type: DendronASTTypes.REF_LINK_V2,
data: {
link,
},
value,
};
return eat(match[0])(refNote);
} else {
const link = LinkUtils.parseNoteRef(linkMatch);
// If the link is same file [[#header]], it's implicitly to the same file it's located in
if (link.from?.fname === "")
link.from.fname = MDUtilsV5.getProcData(proc).fname;
const { value } = LinkUtils.parseLink(linkMatch);
const refNote: NoteRefNoteV4 = {
type: DendronASTTypes.REF_LINK_V2,
data: {
link,
},
value,
};
return eat(match[0])(refNote);
}
}
return;
}
inlineTokenizer.locator = locator;
inlineTokenizerV5.locator = locator;
const Parser = proc.Parser;
const inlineTokenizers = Parser.prototype.inlineTokenizers;
const inlineMethods = Parser.prototype.inlineMethods;
if (MDUtilsV5.isV5Active(proc)) {
inlineTokenizers.refLinkV2 = inlineTokenizerV5;
inlineMethods.splice(inlineMethods.indexOf("link"), 0, "refLinkV2");
} else {
inlineTokenizers.refLinkV2 = inlineTokenizer;
inlineMethods.splice(inlineMethods.indexOf("link"), 0, "refLinkV2");
}
return Parser;
}