Skip to content
This repository has been archived by the owner on Nov 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #34 from spring-media/embed-content-renderer
Browse files Browse the repository at this point in the history
Add embed-content component for embedding arbitrary html content
  • Loading branch information
preventdefault authored Jun 5, 2020
2 parents d4c9fa0 + 0646be8 commit b92cd9d
Show file tree
Hide file tree
Showing 39 changed files with 148 additions and 562 deletions.
92 changes: 1 addition & 91 deletions src/embed-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ loadScript('https://some-domain.com/some-script.js').then(() => console.log('loa
### `getScriptSrcFromOembedHTML`

```typescript
getScriptSrcFromOembedHTML(html: string, fallback: string): string | null
getScriptSrcFromOembedHTML(html: string): string | null
```

Get the script source from given (oEmbed) html string.
Expand All @@ -33,93 +33,3 @@ const someOembedHTML = '<div></div><script async src="https://some-oembed-provid

console.log(getScriptSrcFromOembedHTML(someOembedHTML, 'https://domain.com/fallback.js')); // https://some-oembed-provider.com
```

## Instagram

### `libraryIsAvailable`

```typescript
libraryIsAvailable(): boolean
````

Checks whether the Instagram embeds library (`window.instgrm.Embeds`) is present.

```javascript
import { instagram } from '@spring-media/red-sourcepoint-cmp/dist/esm/embed-utils';
console.log(instagram.libraryIsAvailable())
```

### `processEmbeds`

```typescript
processEmbeds(): void
```

Calls `window.instgrm.Embeds.process()` if available.

```javascript
import { instagram } from '@spring-media/red-sourcepoint-cmp/dist/esm/embed-utils';
instagram.processEmbeds();
```

## Twitter

### `libraryIsAvailable`

```typescript
libraryIsAvailable(): boolean
````
Checks whether the Twitter embeds library (`window.twttr.widgets`) is present.
```javascript
import { twitter } from '@spring-media/red-sourcepoint-cmp/dist/esm/embed-utils';

console.log(twitter.libraryIsAvailable())
```

### `processEmbeds`

```typescript
processEmbeds(): void
```

Calls `window.twttr.widgets.load()` if available.

```javascript
import { twitter } from '@spring-media/red-sourcepoint-cmp/dist/esm/embed-utils';

twitter.processEmbeds(/* optional html element */);
```

## Iframely

### `libraryIsAvailable`

```typescript
libraryIsAvailable(): boolean
````

Checks whether the Iframely embeds library (`window.iframely`) is present.

```javascript
import { iframely } from '@spring-media/red-sourcepoint-cmp/dist/esm/embed-utils';
console.log(iframely.libraryIsAvailable())
```

### `processEmbeds`

```typescript
processEmbeds(): void
```

Calls `window.iframely.load()` if available.

```javascript
import { iframely } from '@spring-media/red-sourcepoint-cmp/dist/esm/embed-utils';
iframely.processEmbeds();
```
22 changes: 0 additions & 22 deletions src/embed-utils/embeds.ts

This file was deleted.

49 changes: 0 additions & 49 deletions src/embed-utils/iframely.test.ts

This file was deleted.

15 changes: 0 additions & 15 deletions src/embed-utils/iframely.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/embed-utils/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ describe('embed-utils index', () => {
const imports = require('./index');
const keys = Object.keys(imports);

expect(keys).toEqual(['instagram', 'twitter', 'iframely', 'getScriptSrcFromOembedHTML', 'loadScript']);
expect(keys).toEqual(['getScriptSrcFromOembedHTML', 'loadScript']);
});
});
3 changes: 0 additions & 3 deletions src/embed-utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
export * as instagram from './instagram';
export * as twitter from './twitter';
export * as iframely from './iframely';
export * from './oembed';
export * from './script-loader';
61 changes: 0 additions & 61 deletions src/embed-utils/instagram.test.ts

This file was deleted.

15 changes: 0 additions & 15 deletions src/embed-utils/instagram.ts

This file was deleted.

20 changes: 10 additions & 10 deletions src/embed-utils/oembed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { getScriptSrcFromOembedHTML } from './oembed';
describe('oembed utilities', () => {
describe('getScriptSrcFromOembedHTML', () => {
it.each([
['script.js', '<script async src="script.js"></script>', 'fallback'],
['script.js', '<script async src="script.js"/>', 'fallback'],
['script.js', '<script async src=" script.js "></script>', 'fallback'],
['script.js', '<script async src= " script.js "></script>', 'fallback'],
['script.js', `<script async src='script.js'></script>`, 'fallback'],
['fallback', `<scrip async src='script.js'></scrip>`, 'fallback'],
['fallback', '<script src="script.js"></script>', 'fallback'],
['fallback', '', 'fallback'],
])('should return %s for input: %s', (expected, input, fallback) => {
expect(getScriptSrcFromOembedHTML(input, fallback)).toBe(expected);
['script.js', '<script async src="script.js"></script>'],
['script.js', '<script async src="script.js"/>'],
['script.js', '<script async src=" script.js "></script>'],
['script.js', '<script async src= " script.js "></script>'],
['script.js', `<script async src='script.js'></script>`],
[null, `<scrip async src='script.js'></scrip>`],
[null, '<script src="script.js"></script>'],
[null, ''],
])('should return %s for input: %s', (expected, input) => {
expect(getScriptSrcFromOembedHTML(input)).toBe(expected);
});
});
});
4 changes: 2 additions & 2 deletions src/embed-utils/oembed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const getScriptSrcFromOembedHTML = (html: string, fallback: string): string => {
export const getScriptSrcFromOembedHTML = (html: string): string | null => {
const src = html.match(/script async src\s*=\s*["'](.+?)["']/);

return src ? src[1].trim() : fallback;
return src ? src[1].trim() : null;
};
4 changes: 4 additions & 0 deletions src/embed-utils/script-loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export const loadScript = (src: string): Promise<void> => {
return new Promise((resolve, reject) => {
if (document.head.querySelector(`[src="${src}"]`)) {
return resolve();
}

const script = document.createElement('script');

script.async = true;
Expand Down
Loading

0 comments on commit b92cd9d

Please sign in to comment.