Skip to content

Commit 239d27a

Browse files
committed
feat: add markdownify functionality
1 parent 514ba6f commit 239d27a

File tree

4 files changed

+108
-3
lines changed

4 files changed

+108
-3
lines changed

scrapegraph-js/README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ yarn add scrapegraph-js
3636

3737
```javascript
3838
import { smartScraper } from 'scrapegraph-js';
39-
import 'dotenv/config';
4039

4140
// Initialize variables
4241
const apiKey = process.env.SGAI_APIKEY; // Set your API key as an environment variable
@@ -85,7 +84,6 @@ Here is a real-world example:
8584
```javascript
8685
import { smartScraper } from 'scrapegraph-js';
8786
import { z } from 'zod';
88-
import 'dotenv/config';
8987

9088
const apiKey = 'your-api-key';
9189
const url = 'https://scrapegraphai.com/';
@@ -107,6 +105,25 @@ const schema = z.object({
107105
})();
108106
```
109107

108+
### Markdownify
109+
Converts a webpage into clean, well-structured markdown format.
110+
```javascript
111+
import { smartScraper } from 'scrapegraph-js';
112+
113+
const apiKey = "your_api_key";
114+
const url = 'https://scrapegraphai.com/';
115+
116+
(async () => {
117+
try {
118+
const response = await markdownify(apiKey, url);
119+
console.log(response);
120+
} catch (error) {
121+
console.error(error);
122+
}
123+
})();
124+
```
125+
126+
110127
### Checking API Credits
111128

112129
```javascript
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { getMarkdownifyRequest, markdownify } from 'scrapegraph-js';
2+
import fs from 'fs';
3+
import 'dotenv/config';
4+
5+
// markdownify function example
6+
const apiKey = process.env.SGAI_APIKEY;
7+
const url = 'https://scrapegraphai.com/';
8+
9+
try {
10+
const response = await markdownify(apiKey, url);
11+
console.log(response);
12+
saveFile(response.result);
13+
} catch (error) {
14+
console.error(error);
15+
}
16+
17+
// helper function for save the file locally
18+
function saveFile(output) {
19+
try {
20+
fs.writeFileSync('result.md', output);
21+
console.log('Success!');
22+
} catch (err) {
23+
console.error('Error during the file writing: ', err);
24+
}
25+
}
26+
27+
// getMarkdownifyRequest function example
28+
const requestId = '2563b972-cb6f-400b-be76-edb235458560';
29+
30+
try {
31+
const response = await getMarkdownifyRequest(apiKey, requestId);
32+
console.log(response);
33+
} catch (error) {
34+
console.log(error);
35+
}

scrapegraph-js/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { smartScraper, getSmartScraperRequest } from './src/smartScraper.js';
2+
export { markdownify, getMarkdownifyRequest } from './src/markdownify.js';
23
export { getCredits } from './src/credits.js';
3-
export { sendFeedback } from './src/feedback.js';
4+
export { sendFeedback } from './src/feedback.js';

scrapegraph-js/src/markdownify.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import axios from 'axios';
2+
import handleError from './utils/handleError.js';
3+
4+
/**
5+
* Converts a webpage into clean, well-structured markdown format.
6+
*
7+
* @param {string} apiKey - Your ScrapeGraph AI API key.
8+
* @param {string} url - The URL of the webpage to be converted.
9+
* @returns {Promise<string>} A promise that resolves to the markdown representation of the webpage.
10+
* @throws {Error} Throws an error if the HTTP request fails.
11+
*/
12+
export async function markdownify(apiKey, url){
13+
const endpoint = 'https://api.scrapegraphai.com/v1/markdownify';
14+
const headers = {
15+
'accept': 'application/json',
16+
'SGAI-APIKEY': apiKey,
17+
};
18+
19+
const payload = {
20+
website_url: url,
21+
};
22+
23+
try {
24+
const response = await axios.post(endpoint, payload, { headers });
25+
return response.data;
26+
} catch (error) {
27+
handleError(error)
28+
}
29+
}
30+
31+
/**
32+
* Retrieves the status or result of a markdownify request, with the option to review results from previous requests.
33+
*
34+
* @param {string} apiKey - Your ScrapeGraph AI API key.
35+
* @param {string} requestId - The unique identifier for the markdownify request whose result you want to retrieve.
36+
* @returns {Promise<string>} A promise that resolves with details about the status or outcome of the specified request.
37+
* @throws {Error} Throws an error if the HTTP request fails.
38+
*/
39+
export async function getMarkdownifyRequest(apiKey, requestId){
40+
const endpoint = 'https://api.scrapegraphai.com/v1/markdownify/' + requestId;
41+
const headers = {
42+
'accept': 'application/json',
43+
'SGAI-APIKEY': apiKey,
44+
};
45+
46+
try {
47+
const response = await axios.get(endpoint, { headers });
48+
return response.data;
49+
} catch (error) {
50+
handleError(error)
51+
}
52+
}

0 commit comments

Comments
 (0)