Skip to content

Commit

Permalink
feat: add TextAnnotation and export it
Browse files Browse the repository at this point in the history
this allows us to handle data set data
by inserting text block boundaries
between items the text at rest can be read correctly
  • Loading branch information
tim-evans authored and colin-alexa committed Mar 21, 2024
1 parent d923618 commit a848b44
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/@atjson/document/src/annotations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from "./inline";
export * from "./object";
export * from "./parse";
export * from "./slice";
export * from "./text";
export * from "./unknown";
12 changes: 12 additions & 0 deletions packages/@atjson/document/src/annotations/text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { BlockAnnotation } from "./block";

/**
* Text annotations are used to chunk text that
* may have jagged edges or should have some
* chunking that would otherwise collapse text
* into run-on text.
*/
export class TextAnnotation extends BlockAnnotation<{}> {
static vendorPrefix = "atjson";
static type = "text";
}
8 changes: 7 additions & 1 deletion packages/@atjson/document/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Insertion,
ParseAnnotation,
SliceAnnotation,
TextAnnotation,
UnknownAnnotation,
} from "./internals";

Expand Down Expand Up @@ -735,7 +736,12 @@ export class Document {
annotation: Annotation<any> | AnnotationJSON
): Annotation<any> {
let DocumentClass = this.constructor as typeof Document;
let schema = [...DocumentClass.schema, ParseAnnotation, SliceAnnotation];
let schema = [
...DocumentClass.schema,
ParseAnnotation,
SliceAnnotation,
TextAnnotation,
];

if (annotation instanceof UnknownAnnotation) {
let KnownAnnotation = schema.find(function annotationMatchesClass(
Expand Down
2 changes: 2 additions & 0 deletions packages/@atjson/document/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ObjectAnnotation,
ParseAnnotation,
SliceAnnotation,
TextAnnotation,
UnknownAnnotation,
is,
deserialize,
Expand Down Expand Up @@ -46,6 +47,7 @@ export {
ObjectAnnotation,
ParseAnnotation,
SliceAnnotation,
TextAnnotation,
UnknownAnnotation,
is,
deserialize,
Expand Down
12 changes: 4 additions & 8 deletions packages/@atjson/document/src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
JSONObject,
Annotation,
SliceAnnotation,
TextAnnotation,
UnknownAnnotation,
is,
withStableIds,
Expand Down Expand Up @@ -128,11 +129,6 @@ const START_TOKENS = [
TokenType.PARSE_START,
];

class Text extends BlockAnnotation {
static vendorPrefix = "atjson";
static type = "text";
}

class Root extends BlockAnnotation {
static vendorPrefix = "atjson";
static type = "root";
Expand Down Expand Up @@ -466,7 +462,7 @@ export function serialize(
}

// Insert text block
let text = new Text({
let text = new TextAnnotation({
start,
end,
});
Expand All @@ -477,15 +473,15 @@ export function serialize(
annotation: text,
selfClosing: false,
shared,
edgeBehaviour: Text.edgeBehaviour,
edgeBehaviour: TextAnnotation.edgeBehaviour,
});
tokens.splice(startIndex + 1, 0, {
type: TokenType.BLOCK_START,
index: text.start,
annotation: text,
selfClosing: false,
shared,
edgeBehaviour: Text.edgeBehaviour,
edgeBehaviour: TextAnnotation.edgeBehaviour,
});
}
textLength = 0;
Expand Down

0 comments on commit a848b44

Please sign in to comment.