-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
99 lines (77 loc) · 2.66 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
use GuzzleHttp\Client;
use League\CommonMark\CommonMarkConverter;
require_once 'vendor/autoload.php';
// DcWiZBvp6PLEDWHwhf0L6MuJcPNtzmdTocW3jGmM
$url = $argv[1];
$application = $argv[2];
$token = $argv[3];
$index = 'sigmie-com-docs';
$client = new Client([
// Base URI is used with relative requests
'base_uri' => $url,
// You can set any number of default request options.
'timeout' => 20.0,
'http_errors' => false,
'headers' => [
'Content-Type' => 'application/json',
'X-Sigmie-API-Key' => $token,
'X-Sigmie-Application' => $application,
]
]);
// $res = $client->post("/v1/index/{$index}");
$res = $client->post("/v1/index/{$index}/clear");
//dd($res->getStatusCode(), json_decode($res->getBody()->getContents(), true));
$parsedown = new CommonMarkConverter();
$filesSubdir = glob('src/pages/docs/**/*.{md}', GLOB_BRACE);
$filesDir = glob('src/pages/docs/*.{md}', GLOB_BRACE);
$files = [...$filesSubdir, ...$filesDir];
$json = [];
foreach ($files as $file) {
$markdown = file_get_contents($file);
$content = $parsedown->convert($markdown)->getContent();
if (empty($content)) {
continue;
}
$path = str_replace('src/pages/', '', $file);
$path = str_replace('.md', '', $path);
// $titles = getTextBetweenTags($content, 'h1');
$subtitles = getTextBetweenTags($content, 'h2');
$headings = getTextBetweenTags($content, 'h3');
$body = getTextBetweenTags($content, 'p');
$lis = getTextBetweenTags($content, 'li');
preg_match('/title:( )?(.*)\n/', $subtitles[0], $matches);
$title = $matches[2] ?? $matches[1];
preg_match('/description:( )?(.*)/', $subtitles[0], $matches);
$description = $matches[2] ?? $matches[1];
array_splice($subtitles, 0, 1);
//Remove callouts
$body = preg_replace('/{% ?(\/)?callout[a-zA-Z=" ]*%}/', '', $body);
$json[] = [
'action' => 'create',
'body' => [
'title' => $title,
'description' => $description,
'subtitles' => $subtitles,
'h3' => $headings,
'content' => $body,
'li' => $lis,
'path' => $path,
'source' => 'docs.sigmie.com'
],
];
}
$res = $client->put("v1/index/{$index}/batch", ['json' => $json,]);
dd($res->getStatusCode(), json_decode($res->getBody()->getContents(), true));
// $json = json_decode($res->getBody()->getContents(), true);
// dd($json);
function getTextBetweenTags($string, $tagname)
{
$d = new DOMDocument();
$d->loadHTML($string);
$return = array();
foreach ($d->getElementsByTagName($tagname) as $item) {
$return[] = $item->textContent;
}
return $return;
}