58 lines
1.5 KiB
JavaScript
58 lines
1.5 KiB
JavaScript
import { findAllEntities } from "../core";
|
|
const mapEntityToToken = (entity) => {
|
|
if (entity.type === "mention") {
|
|
return {
|
|
kind: "mention",
|
|
entity,
|
|
};
|
|
}
|
|
if (entity.type === "tag") {
|
|
return {
|
|
kind: "tag",
|
|
entity,
|
|
};
|
|
}
|
|
return {
|
|
kind: "link",
|
|
entity,
|
|
};
|
|
};
|
|
const createTextToken = (text, start, end) => ({
|
|
kind: "text",
|
|
text,
|
|
start,
|
|
end,
|
|
});
|
|
export const createAngularContentTokens = (inputText) => {
|
|
const text = inputText ?? "";
|
|
const entities = findAllEntities(text);
|
|
let cursor = 0;
|
|
const tokens = [];
|
|
for (const entity of entities) {
|
|
if (entity.start > cursor) {
|
|
tokens.push(createTextToken(text.slice(cursor, entity.start), cursor, entity.start));
|
|
}
|
|
tokens.push(mapEntityToToken(entity));
|
|
cursor = entity.end;
|
|
}
|
|
if (cursor < text.length) {
|
|
tokens.push(createTextToken(text.slice(cursor), cursor, text.length));
|
|
}
|
|
return tokens;
|
|
};
|
|
export const buildAngularTagHref = (entity) => {
|
|
return `/search/?query=${encodeURIComponent(entity.tag.toLowerCase())}`;
|
|
};
|
|
export class AngularContentSuggestionsAdapter {
|
|
snapshot(inputText) {
|
|
const text = inputText ?? "";
|
|
const entities = findAllEntities(text);
|
|
const tokens = createAngularContentTokens(text);
|
|
return {
|
|
text,
|
|
entities,
|
|
tokens,
|
|
};
|
|
}
|
|
}
|
|
//# sourceMappingURL=tokens.js.map
|