Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding links if server responds with link and allow HTTP headers #57

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const editor = EditorJS({
linkTool: {
class: LinkTool,
config: {
endpoint: 'http://localhost:8008/fetchUrl', // Your backend endpoint for url data fetching
endpoint: 'http://localhost:8008/fetchUrl', // Your backend endpoint for url data fetching,
}
}
},
Expand All @@ -70,6 +70,7 @@ Link Tool supports these configuration parameters:
| Field | Type | Description |
| ---------|-------------|------------------------------------------------|
| endpoint | `string` | **Required:** the endpoint for link data fetching. |
| headers | `object` | **Optional:** the headers used in the GET request. |

## Output data

Expand Down Expand Up @@ -107,6 +108,7 @@ Backend response **should** cover following format:
```json5
{
"success" : 1,
"link": "https://codex.so", // Optionally return a link to set the hyperlink URL
"meta": {
// ... any fields you want
}
Expand All @@ -115,6 +117,8 @@ Backend response **should** cover following format:

**success** — uploading status. 1 for successful, 0 for failed

**link** - You are able to return the l

**meta** — link fetched data.

Currently, the plugin's design supports the 'title', 'image', and 'description' fields. They should have the following format in the response:
Expand Down
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default class LinkTool {
*/
this.config = {
endpoint: config.endpoint || '',
headers: config.headers || {},
};

this.nodes = {
Expand Down Expand Up @@ -385,6 +386,7 @@ export default class LinkTool {
try {
const { body } = await (ajax.get({
url: this.config.endpoint,
headers: this.config.headers,
data: {
url,
},
Expand All @@ -410,7 +412,12 @@ export default class LinkTool {

const metaData = response.meta;

this.data = { meta: metaData };
const link = response.link || this.data.link;

this.data = {
meta: metaData,
link,
};

if (!metaData) {
this.fetchingFailed(this.api.i18n.t('Wrong response format from the server'));
Expand Down