Skip to content

Commit d5eb6aa

Browse files
Merge pull request #215 from rakutentech/feature/anchor-url
Fixes #211 and #212 where anchor link and markdown indentation didn't work as intended
2 parents a027937 + f771cc6 commit d5eb6aa

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

src/LaravelRequestDocs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public function lrdDocComment(string $docComment): string
295295
}
296296
if ($counter == 1 && !Str::contains($comment, '@lrd')) {
297297
if (Str::startsWith($comment, '*')) {
298-
$comment = trim(substr($comment, 1));
298+
$comment = substr($comment, 1);
299299
}
300300
// remove first character from string
301301
$lrdComment .= $comment . "\n";

tests/mocks/lrd-response.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"nullable|integer|min:1|max:100"
2828
]
2929
},
30-
"doc_block": "#Hello markdown\n## Documentation for /my route\n",
30+
"doc_block": " #Hello markdown\n ## Documentation for /my route\n",
3131
"responses": [
3232
"200",
3333
"400",
@@ -69,7 +69,7 @@
6969
"nullable|integer|min:1|max:100"
7070
]
7171
},
72-
"doc_block": "#Hello markdown\n## Documentation for /my route\n",
72+
"doc_block": " #Hello markdown\n ## Documentation for /my route\n",
7373
"responses": [
7474
"200",
7575
"400",
@@ -111,7 +111,7 @@
111111
"nullable|integer|min:1|max:100"
112112
]
113113
},
114-
"doc_block": "#Hello markdown\n## Documentation for /my route\n",
114+
"doc_block": " #Hello markdown\n ## Documentation for /my route\n",
115115
"responses": [
116116
"200",
117117
"400",
@@ -153,7 +153,7 @@
153153
"nullable|integer|min:1|max:100"
154154
]
155155
},
156-
"doc_block": "#Hello markdown\n## Documentation for /my route\n",
156+
"doc_block": " #Hello markdown\n ## Documentation for /my route\n",
157157
"responses": [
158158
"200",
159159
"400",

ui/src/components/ApiAction.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ export default function ApiAction(props: Props) {
145145
// do nothing
146146
}
147147

148+
const wasDataWrapped = (data && data._lrd)
149+
148150
if (data && data._lrd && data._lrd.queries) {
149151
const sqlQueries = data._lrd.queries.map((query: any) => {
150152
return "Connection: "
@@ -174,12 +176,12 @@ export default function ApiAction(props: Props) {
174176
})
175177
}
176178
if (isJson) {
177-
if (data?.data) {
179+
if (wasDataWrapped && data?.data) {
178180
setResponseData(JSON.stringify(data?.data, null, 2))
179181
} else {
180182
setResponseData(JSON.stringify(data, null, 2))
181183
}
182-
184+
183185
} else {
184186
setResponseData(dataString)
185187
}

ui/src/components/App.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ export default function App() {
5959
generateDocs(api)
6060
}, [])
6161

62+
const scrollToAnchorOnHistory = () => {
63+
// get the anchor link and scroll to it
64+
const anchor = window.location.hash;
65+
if (anchor) {
66+
const anchorId = anchor.replace('#', '');
67+
const element = document.getElementById(anchorId);
68+
if (element) {
69+
element.scrollIntoView();
70+
}
71+
}
72+
}
73+
6274
const generateDocs = (url: string) => {
6375
setSendingRequest(true)
6476
const response = fetch(url);
@@ -75,6 +87,9 @@ export default function App() {
7587
setLrdDocsJson(lrdDocsJson)
7688
setLrdDocsJsonCopy(lrdDocsJson)
7789
setSendingRequest(false)
90+
setTimeout(() => {
91+
scrollToAnchorOnHistory()
92+
}, 10) // greater than 1 is fine
7893
}).catch((error) => {
7994
setError(error.message)
8095
setSendingRequest(false)

ui/src/components/Sidebar.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ export default function Sidebar(props: Props) {
2828
</li>
2929
)}
3030
<li>
31-
<AnchorLink href={'#' + lrdDocsItem.http_method + lrdDocsItem.uri} className="flex flex-row px-0 py-1">
31+
<AnchorLink href={'#' + lrdDocsItem.http_method + lrdDocsItem.uri}
32+
onClick={() => {
33+
window.history.pushState({}, '', '#' + lrdDocsItem.http_method + lrdDocsItem.uri);
34+
}}
35+
className="flex flex-row px-0 py-1">
3236
<span className={`method-${lrdDocsItem.http_method} uppercase text-xs w-12 p-0 flex flex-row-reverse`}>
3337
{lrdDocsItem.http_method}
3438
</span>

0 commit comments

Comments
 (0)