Skip to content

Commit

Permalink
fix: modification to handle instagram caption
Browse files Browse the repository at this point in the history
  • Loading branch information
a-rena committed Jul 13, 2023
1 parent adc6a99 commit 41e05bf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ export function identify(url: IUrl): {
width?: string;
height?: string;
sandbox?: string;
captioned?: boolean;
};
Class: typeof IframeEmbed;
} | null {
Expand Down
38 changes: 38 additions & 0 deletions packages/@atjson/source-html/src/converter/social-embeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ function isBlockquoteEmbed(annotation: Annotation<any>) {
);
}

interface attribute {
url: string;
width?: string;
height?: string;
sandbox?: string;
captioned?: boolean;
}

function isFacebookDiv(annotation: Annotation<any>) {
if (annotation.type !== "div") {
return false;
Expand All @@ -48,6 +56,18 @@ function identifyURL(src: string) {
return SocialURLs.identify(url);
}

function checkInstagramCaption(
canonicalURL: attribute,
obj: object
): attribute {
for (let dataset in obj) {
if (dataset == "instgrm-captioned") {
canonicalURL.captioned = true;
}
}
return canonicalURL;
}

export default function (doc: Document) {
/**
* Instagram/Twitter embeds in blockquotes:
Expand Down Expand Up @@ -92,12 +112,29 @@ export default function (doc: Document) {
}
if (canonicalURL == null) {
for (let link of links) {
console.log(
"Test -------->",
identifyURL(link.attributes.href),
"blockquote",
blockquote
);
canonicalURL = identifyURL(link.attributes.href);
if (canonicalURL) {
break;
}
}
}
if (
canonicalURL &&
canonicalURL.attributes !== null &&
blockquote.attributes.dataset
) {
const instaAttributes = checkInstagramCaption(
canonicalURL?.attributes,
blockquote.attributes.dataset
);
canonicalURL.attributes = instaAttributes;
}

if (canonicalURL) {
let { attributes, Class } = canonicalURL;
Expand All @@ -108,6 +145,7 @@ export default function (doc: Document) {
return start <= annotation.start && annotation.end <= end;
})
.remove();
console.log("attributes --->", canonicalURL);
doc.addAnnotations(
new ParseAnnotation({
start,
Expand Down
2 changes: 2 additions & 0 deletions packages/@atjson/source-html/test/converter-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ describe("@atjson/source-html", () => {
"blocks": [
{
"attributes": {
"captioned": true,
"url": "https://www.instagram.com/tv/B95M4kNhbzz",
},
"id": "B00000000",
Expand All @@ -657,6 +658,7 @@ describe("@atjson/source-html", () => {
"blocks": [
{
"attributes": {
"captioned": true,
"url": "https://www.instagram.com/reel/CDt37vzFw3f",
},
"id": "B00000000",
Expand Down

0 comments on commit 41e05bf

Please sign in to comment.