|
2 | 2 |
|
3 | 3 | namespace App\Console\Commands;
|
4 | 4 |
|
| 5 | +use App\Support\Markdown; |
5 | 6 | use DOMDocument;
|
6 | 7 | use DOMNode;
|
7 | 8 | use Illuminate\Console\Command;
|
8 |
| -use League\HTMLToMarkdown\Converter\ConverterInterface; |
9 |
| -use League\HTMLToMarkdown\ElementInterface; |
10 |
| -use League\HTMLToMarkdown\HtmlConverter; |
11 |
| -use Illuminate\Support\Str; |
12 |
| -use SebastianBergmann\CodeCoverage\Report\PHP; |
13 | 9 |
|
14 | 10 | class GenerateLlmsTxt extends Command
|
15 | 11 | {
|
@@ -51,127 +47,7 @@ public function writePage(string $url)
|
51 | 47 |
|
52 | 48 | $page = file_get_contents(resource_path('js/Pages/' . $url . '.jsx'));
|
53 | 49 |
|
54 |
| - $replace = [ |
55 |
| - '<>' => '', |
56 |
| - '</>' => '', |
57 |
| - "{' '}" => ' ', |
58 |
| - '<Link' => '<a', |
59 |
| - '</Link>' => '</a>', |
60 |
| - '<Notice>' => '<p>', |
61 |
| - '</Notice>' => '</p>', |
62 |
| - ]; |
63 |
| - |
64 |
| - $codeBlockType = null; |
65 |
| - $inFencedCodeBlock = false; |
66 |
| - |
67 |
| - $page = str($page) |
68 |
| - ->after('return (') |
69 |
| - ->beforeLast(')') |
70 |
| - ->replace(array_keys($replace), array_values($replace)) |
71 |
| - ->replaceMatches('/className=\{[^}]*\}/', '') |
72 |
| - ->explode(PHP_EOL) |
73 |
| - ->map(function ($line) use (&$codeBlockType, &$inFencedCodeBlock) { |
74 |
| - if (str_contains($line, '<TabbedCode')) { |
75 |
| - $codeBlockType = 'tabbedcode'; |
76 |
| - |
77 |
| - $this->currentCodeBlockId = Str::random(10); |
78 |
| - $this->tabbedCodeBlocks[$this->currentCodeBlockId] = $line; |
79 |
| - |
80 |
| - return '<p><tabbedcode>' . $this->currentCodeBlockId . '</tabbedcode></p>'; |
81 |
| - } |
82 |
| - |
83 |
| - if (str_contains($line, '<CodeBlock')) { |
84 |
| - $codeBlockType = 'codeblock'; |
85 |
| - |
86 |
| - $this->currentCodeBlockId = Str::random(10); |
87 |
| - $this->codeBlocks[$this->currentCodeBlockId] = $line; |
88 |
| - |
89 |
| - return '<p><codeblock>' . $this->currentCodeBlockId . '</codeblock></p>'; |
90 |
| - } |
91 |
| - |
92 |
| - if ($codeBlockType === null) { |
93 |
| - return str($line)->replaceMatches('/\s+/', ' ')->replaceMatches('/\{\'<(.+)>\'\}/', '<$1>')->trim()->toString(); |
94 |
| - } |
95 |
| - |
96 |
| - if (str_contains($line, '`')) { |
97 |
| - $inFencedCodeBlock = !$inFencedCodeBlock; |
98 |
| - } |
99 |
| - |
100 |
| - try { |
101 |
| - if ($codeBlockType === 'tabbedcode') { |
102 |
| - $this->tabbedCodeBlocks[$this->currentCodeBlockId] .= $line; |
103 |
| - } else { |
104 |
| - $this->codeBlocks[$this->currentCodeBlockId] .= $line; |
105 |
| - } |
106 |
| - } catch (\Exception $e) { |
107 |
| - dd($this->tabbedCodeBlocks, $this->codeBlocks, $codeBlockType, $line); |
108 |
| - } |
109 |
| - |
110 |
| - if ($inFencedCodeBlock) { |
111 |
| - // Don't look for ending tag inside fenced code blocks |
112 |
| - return null; |
113 |
| - } |
114 |
| - |
115 |
| - if (str_contains($line, '/>')) { |
116 |
| - $codeBlockType = null; |
117 |
| - } |
118 |
| - |
119 |
| - return null; |
120 |
| - }) |
121 |
| - ->filter(fn($line) => $line !== null && trim($line) !== '') |
122 |
| - ->implode(PHP_EOL); |
123 |
| - |
124 |
| - $converter = new HtmlConverter(['header_style' => 'atx', 'hard_break' => true, 'remove_nodes' => 'div']); |
125 |
| - $converter->getEnvironment()->addConverter(new class($this->tabbedCodeBlocks, $this->codeBlocks) implements ConverterInterface { |
126 |
| - public function __construct(protected array $tabbedBlocks, protected array $codeBlocks) |
127 |
| - { |
128 |
| - // |
129 |
| - } |
130 |
| - |
131 |
| - public function convert(ElementInterface $node): string |
132 |
| - { |
133 |
| - if ($node->getTagName() === 'tabbedcode') { |
134 |
| - $content = $this->tabbedBlocks[$node->getValue()]; |
135 |
| - |
136 |
| - return str($content)->after('examples={')->beforeLast('}')->explode('`,')->map(function ($example) { |
137 |
| - if (!str_contains($example, 'code:')) { |
138 |
| - return null; |
139 |
| - } |
140 |
| - |
141 |
| - preg_match("/language: '([^']+)'/", $example, $matches); |
142 |
| - $language = $matches[1]; |
143 |
| - |
144 |
| - preg_match("/name: '([^']+)'/", $example, $matches); |
145 |
| - $name = $matches[1]; |
146 |
| - |
147 |
| - preg_match("/description: '([^']+)'/m", $example, $matches); |
148 |
| - $description = $matches[1] ?? null; |
149 |
| - |
150 |
| - $code = str($example)->after('dedent`')->beforeLast('`,')->trim()->replaceMatches('/\s{7,}/', PHP_EOL)->toString(); |
151 |
| - |
152 |
| - return sprintf("%s%s:\n\n```%s\n%s\n```", $name, $description ? ' (' . $description . ')' : '', $language, $code); |
153 |
| - })->filter()->implode(PHP_EOL . PHP_EOL); |
154 |
| - } |
155 |
| - |
156 |
| - $content = $this->codeBlocks[$node->getValue()]; |
157 |
| - preg_match('/language="([^"]+)"/', $content, $matches); |
158 |
| - $language = $matches[1]; |
159 |
| - preg_match('/dedent`([^`]+)`/', $content, $matches); |
160 |
| - $code = str($matches[1])->trim()->replaceMatches('/\s{7,}/', PHP_EOL)->toString(); |
161 |
| - |
162 |
| - return sprintf("```%s\n%s\n```", $language, $code); |
163 |
| - } |
164 |
| - |
165 |
| - public function getSupportedTags(): array |
166 |
| - { |
167 |
| - return [ |
168 |
| - 'tabbedcode', |
169 |
| - 'codeblock', |
170 |
| - ]; |
171 |
| - } |
172 |
| - }); |
173 |
| - |
174 |
| - $markdown = $converter->convert($page); |
| 50 | + $markdown = Markdown::fromJsx($page); |
175 | 51 |
|
176 | 52 | $this->full[] = $markdown;
|
177 | 53 | }
|
|
0 commit comments