Skip to content

Commit b461b50

Browse files
committed
Add support for appending/prepending markdown content (closes #262)
1 parent 5e4ccbd commit b461b50

File tree

6 files changed

+26
-6
lines changed

6 files changed

+26
-6
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ $ php artisan api:update
240240

241241
As an optional parameter, you can use `--location` to tell the update command where your documentation can be found.
242242

243+
## Automatically add markdown to the beginning or end of the documentation
244+
If you wish to automatically add the same content to the docs every time you generate, you can add a `prepend.md` and/or `append.md` file to the source folder, and they will be included above and below the generated documentation.
245+
246+
**File locations:**
247+
- `public/docs/source/prepend.md`
248+
- `public/docs/source/append.md`
249+
243250
## Skip single routes
244251

245252
If you want to skip a single route from a list of routes that match a given prefix, you can use the `@hideFromAPIDocumentation` tag on the Controller method you do not want to document.

src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ private function writeMarkdown($parsedRoutes)
111111
$outputPath = $this->option('output');
112112
$targetFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'index.md';
113113
$compareFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'.compare.md';
114+
$prependFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'prepend.md';
115+
$appendFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'append.md';
114116

115117
$infoText = view('apidoc::partials.info')
116118
->with('outputPath', ltrim($outputPath, 'public/'))
@@ -156,12 +158,19 @@ private function writeMarkdown($parsedRoutes)
156158
});
157159
}
158160

161+
$prependFileContents = file_exists($prependFile)
162+
?file_get_contents($prependFile)."\n" : '';
163+
$appendFileContents = file_exists($appendFile)
164+
? "\n".file_get_contents($appendFile) : '';
165+
159166
$documentarian = new Documentarian();
160167

161168
$markdown = view('apidoc::documentarian')
162169
->with('writeCompareFile', false)
163170
->with('frontmatter', $frontmatter)
164171
->with('infoText', $infoText)
172+
->with('prependMd', $prependFileContents)
173+
->with('appendMd', $appendFileContents)
165174
->with('outputPath', $this->option('output'))
166175
->with('showPostmanCollectionButton', ! $this->option('noPostmanCollection'))
167176
->with('parsedRoutes', $parsedRouteOutput);
@@ -178,6 +187,8 @@ private function writeMarkdown($parsedRoutes)
178187
->with('writeCompareFile', true)
179188
->with('frontmatter', $frontmatter)
180189
->with('infoText', $infoText)
190+
->with('prependMd', $prependFileContents)
191+
->with('appendMd', $appendFileContents)
181192
->with('outputPath', $this->option('output'))
182193
->with('showPostmanCollectionButton', ! $this->option('noPostmanCollection'))
183194
->with('parsedRoutes', $parsedRouteOutput);

src/resources/views/documentarian.blade.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
<!-- START_INFO -->
55
{!! $infoText !!}
66
<!-- END_INFO -->
7-
7+
{!! $prependMd !!}
88
@foreach($parsedRoutes as $group => $routes)
99
@if($group)
1010
#{!! $group !!}
1111
@endif
1212
@foreach($routes as $parsedRoute)
1313
@if($writeCompareFile === true)
14-
{!! $parsedRoute['output']!!}
14+
{!! $parsedRoute['output'] !!}
1515
@else
16-
{!! isset($parsedRoute['modified_output']) ? $parsedRoute['modified_output'] : $parsedRoute['output']!!}
16+
{!! isset($parsedRoute['modified_output']) ? $parsedRoute['modified_output'] : $parsedRoute['output'] !!}
1717
@endif
1818
@endforeach
19-
@endforeach
19+
@endforeach{!! $appendMd !!}

tests/Fixtures/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,4 @@ $.ajax(settings).done(function (response) {
106106

107107
<!-- END_960a1b2b0f0f4dde8ce993307397f9c4 -->
108108

109+

tests/Fixtures/resource_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,4 @@ $.ajax(settings).done(function (response) {
282282

283283
<!-- END_4bb7fb4a7501d3cb1ed21acfc3b205a9 -->
284284

285+

tests/GenerateDocumentationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ public function testCanParseResourceRoutes()
123123
'--routePrefix' => 'api/*',
124124
]);
125125
$fixtureMarkdown = __DIR__.'/Fixtures/resource_index.md';
126-
$gneratedMarkdown = __DIR__.'/../public/docs/source/index.md';
127-
$this->assertFilesHaveSameContent($fixtureMarkdown, $gneratedMarkdown);
126+
$generatedMarkdown = __DIR__.'/../public/docs/source/index.md';
127+
$this->assertFilesHaveSameContent($fixtureMarkdown, $generatedMarkdown);
128128
}
129129

130130
public function testGeneratedMarkdownFileIsCorrect()

0 commit comments

Comments
 (0)