types#Annotation TypeScript Examples
The following examples show how to use
types#Annotation.
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: annotationFileUtils.tsx From obsidian-annotator with GNU Affero General Public License v3.0 | 6 votes |
export async function writeAnnotation(annotation: Annotation, plugin: AnnotatorPlugin, annotationFilePath: string) {
const vault = plugin.app.vault;
const tfile = vault.getAbstractFileByPath(annotationFilePath);
let res: ReturnType<typeof writeAnnotationToAnnotationFileString>;
if (tfile instanceof TFile) {
const text = await vault.read(tfile);
res = writeAnnotationToAnnotationFileString(annotation, text, plugin);
vault.modify(tfile, res.newAnnotationFileString);
} else {
res = writeAnnotationToAnnotationFileString(annotation, null, plugin);
vault.create(annotationFilePath, res.newAnnotationFileString);
}
return res.newAnnotation;
}
Example #2
Source File: annotationUtils.tsx From obsidian-annotator with GNU Affero General Public License v3.0 | 6 votes |
export function getAnnotationFromFileContent(annotationId: string, fileContent: string): Annotation {
const annotationRegex = makeAnnotationBlockRegex(annotationId);
let m: RegExpExecArray;
if ((m = annotationRegex.exec(fileContent)) !== null) {
if (m.index === annotationRegex.lastIndex) {
annotationRegex.lastIndex++;
}
const {
groups: { annotationBlock, annotationId }
} = m;
return getAnnotationFromAnnotationBlock(annotationBlock, annotationId);
} else {
return null;
}
}
Example #3
Source File: annotationUtils.tsx From obsidian-annotator with GNU Affero General Public License v3.0 | 6 votes |
export function writeAnnotationToAnnotationFileString(
annotation: Annotation,
annotationFileString: string | null,
annotatorSettingsObject: IHasAnnotatorSettings
): { newAnnotationFileString: string; newAnnotation: Annotation } {
const annotationId = annotation.id ? annotation.id : Math.random().toString(36).substr(2);
const res = JSON.parse(JSON.stringify(annotation));
res.flagged = false;
res.id = annotationId;
const annotationString = makeAnnotationString(res, annotatorSettingsObject);
if (annotationFileString !== null) {
let didReplace = false;
const regex = makeAnnotationBlockRegex(annotationId);
annotationFileString = annotationFileString.replace(regex, () => {
didReplace = true;
return annotationString;
});
if (!didReplace) {
annotationFileString = `${annotationFileString}\n${annotationString}`;
}
return { newAnnotationFileString: annotationFileString, newAnnotation: res };
} else {
return { newAnnotationFileString: annotationString, newAnnotation: res };
}
}
Example #4
Source File: annotationUtils.tsx From obsidian-annotator with GNU Affero General Public License v3.0 | 6 votes |
getAnnotationFromAnnotationBlock = (annotationBlock: string, annotationId: string): Annotation => {
const contentRegex = makeAnnotationContentRegex();
const content = annotationBlock
.split('\n')
.map(x => x.substr(1))
.join('\n');
const {
groups: { annotationJson, prefix, highlight, postfix, comment, tags }
} = contentRegex.exec(content);
const annotation = JSON.parse(annotationJson);
const annotationTarget = annotation.target?.[0];
if (annotationTarget && 'selector' in annotationTarget) {
annotationTarget.selector = annotationTarget.selector.map(x =>
x.type == 'TextQuoteSelector'
? { ...x, prefix: prefix ?? x.prefix, exact: x.exact ?? highlight, suffix: postfix ?? x.suffix }
: x
);
}
annotation.text = comment;
annotation.tags = tags
.split(',')
.map(x => x.trim().substr(1))
.filter(x => x);
if ('group' in annotation) {
delete annotation.group;
}
return { ...makeDefaultAnnotationObject(annotationId, annotation.tags), ...annotation };
}
Example #5
Source File: annotationUtils.tsx From obsidian-annotator with GNU Affero General Public License v3.0 | 6 votes |
getAnnotationHighlightTextData = (annotation: Annotation) => {
let prefix = '';
let exact = '';
let suffix = '';
annotation.target?.[0]?.selector?.forEach(x => {
if (x.type == 'TextQuoteSelector') {
prefix = x.prefix || '';
exact = x.exact || '';
suffix = x.suffix || '';
}
});
return { prefix, exact, suffix };
}
Example #6
Source File: annotationUtils.tsx From obsidian-annotator with GNU Affero General Public License v3.0 | 6 votes |
makeAnnotationString = (annotation: Annotation, plugin: IHasAnnotatorSettings) => {
const { highlightHighlightedText, includePostfix, includePrefix } = plugin.settings.annotationMarkdownSettings;
const { prefix, exact, suffix } = getAnnotationHighlightTextData(annotation);
const annotationString =
'%%\n```annotation-json' +
`\n${JSON.stringify(
stripDefaultValues(annotation, makeDefaultAnnotationObject(annotation.id, annotation.tags))
)}` +
'\n```\n%%\n' +
`*${includePrefix ? `%%PREFIX%%${prefix.trim()}` : ''}%%HIGHLIGHT%%${
highlightHighlightedText ? ' ==' : ''
}${exact.trim()}${highlightHighlightedText ? '== ' : ''}${
includePostfix ? `%%POSTFIX%%${suffix.trim()}` : ''
}*\n%%LINK%%[[#^${annotation.id}|show annotation]]\n%%COMMENT%%\n${
annotation.text || ''
}\n%%TAGS%%\n${annotation.tags.map(x => `#${x}`).join(', ')}`;
return (
'\n' +
annotationString
.split('\n')
.map(x => `>${x}`)
.join('\n') +
'\n^' +
annotation.id +
'\n'
);
}
Example #7
Source File: annotationFileUtils.tsx From obsidian-annotator with GNU Affero General Public License v3.0 | 5 votes |
export async function getAnnotation(annotationId: string, file: TFile, vault: Vault): Promise<Annotation> {
const text = await vault.read(file);
return getAnnotationFromFileContent(annotationId, text);
}
Example #8
Source File: annotationUtils.tsx From obsidian-annotator with GNU Affero General Public License v3.0 | 5 votes |
export function checkPseudoAnnotationEquality(annotation: Annotation, pseudoAnnotation: Annotation): boolean {
const isPageNote = !annotation.target?.length;
if (isPageNote) {
return false;
}
const selectors = new Set(annotation.target[0].selector.map(x => JSON.stringify(x)));
return pseudoAnnotation?.target?.[0]?.selector?.map(x => selectors.has(JSON.stringify(x))).reduce((a, b) => a || b);
}