Skip to content

Commit da062ae

Browse files
committed
docs: add highlight plugin for prism in docsify and use it where lines have to be added
- enhance documentation
1 parent db34b44 commit da062ae

15 files changed

+163
-19
lines changed

.github_changelog_generator

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ release-branch=master
55
issues-of-open-milestones=false
66
unreleased=false
77
output=docs/CHANGELOG.md
8+
exclude-tags-regex=.*-pre.*

docs/api.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ __Options:__
3939
- `versionPrefix` (*optional*) - Used for automatic tag and name generation for services
4040
- `include` (*optional*) - Object to configure for which services documentation will be generated, empty means all will be included:
4141
- `tags` - Array of tags for that service documentation will be generated
42-
- `paths` - Array of paths (string or regex) for that service documentation will be generated, Notice: paths don't start with /
42+
- `paths` - Array of paths (string or regex) for that service documentation will be generated, __Notice:__ paths don't start with /
43+
- `filter(service, path)` - Function to filter services for that documentation will be generated, __Notice:__ paths don't start with /
4344
- `ignore` (*optional*) - Object to configure to ignore with the following keys:
4445
- `tags` - Array of tags for that no service documentation will be generated
4546
- `paths` - Array of paths (string or regex) for that no service documentation will be generated, Notice: paths don't start with /
47+
- `filter(service, path)` - Function to filter services for that documentation won't be generated, __Notice:__ paths don't start with /
4648
- `appProperty` (*optional*, default: `docs`) - Property of the feathers app object that the generated specification will be saved to, allows custom post-processing; set empty to disable
4749
- `defaults` (*optional*) - Object to customize the defaults for generation of the specification
4850
- `getOperationArgs({ service, path, config, apiPath, version })` - Function to generate args that the methods for operations will consume, can also customize default tag and model generation

docs/custom-methods.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,6 @@ Check out the [example](/examples/custom_methods.md) to see it in action.
109109

110110
## Limitations
111111

112-
- In Feathers 4 the custom methods are registered after the default methods of a service and therefor
113-
the /service/:id route will "win" over a /server/custom route for all methods but POST.
112+
- In Feathers 4 the custom methods are registered after the default methods of a service and therefore
113+
the /service/:id route will "win" over a /service/custom route for all methods but POST.
114114
- At least one default method has to be existent in a service to be registered

docs/docsify/prism-lines.js

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
function install (hook, vm) {
2+
const codeContainer = {
3+
counter: 0,
4+
codes: {}
5+
};
6+
7+
hook.beforeEach(function (content) {
8+
codeContainer.counter = 0;
9+
codeContainer.codes = {};
10+
return content;
11+
});
12+
13+
hook.mounted(function () {
14+
if (vm.compiler._marked.Renderer.prototype.code) {
15+
const original = vm.compiler._marked.defaults.renderer.code;
16+
17+
const code = (code, infostring, escaped) => {
18+
const infostringParts = infostring.split(' ');
19+
let options;
20+
21+
if (infostringParts.length > 1) {
22+
const [lang, ...rest] = infostringParts;
23+
infostring = lang;
24+
try {
25+
options = JSON.parse(rest.join(' '));
26+
} catch (e) {
27+
console.error('invalid code options');
28+
}
29+
}
30+
31+
const originalResult = original(code, infostring, escaped);
32+
33+
if (typeof options === 'object') {
34+
let { lineNumbers, highlight } = options;
35+
36+
if (typeof lineNumbers === 'boolean') {
37+
lineNumbers = { enabled: lineNumbers };
38+
} else if (typeof lineNumbers === 'number') {
39+
lineNumbers = { enabled: true, start: lineNumbers };
40+
}
41+
42+
const additionalAttributes = [];
43+
if (typeof lineNumbers === 'object') {
44+
if (lineNumbers.enabled) {
45+
additionalAttributes.push(`class="language-${infostring} line-numbers"`);
46+
}
47+
if (lineNumbers.start) {
48+
additionalAttributes.push(`data-start="${lineNumbers.start}"`);
49+
}
50+
}
51+
52+
if (typeof highlight === 'string') {
53+
highlight = { line: highlight };
54+
}
55+
56+
if (typeof highlight === 'object') {
57+
if (highlight.line) {
58+
additionalAttributes.push(`data-line="${highlight.line}"`);
59+
}
60+
if (highlight.lineOffset) {
61+
additionalAttributes.push(`data-line-offset="${highlight.lineOffset}"`);
62+
}
63+
}
64+
65+
if (additionalAttributes.length) {
66+
codeContainer.codes[codeContainer.counter] = code;
67+
additionalAttributes.push(`data-code-counter="${codeContainer.counter}"`);
68+
codeContainer.counter += 1;
69+
70+
return originalResult.replace(/^<pre /, `<pre ${additionalAttributes.join(' ')} `);
71+
}
72+
}
73+
74+
return originalResult;
75+
};
76+
77+
vm.compiler._marked.use({ renderer: { code } });
78+
}
79+
});
80+
81+
hook.doneEach(function () {
82+
document.querySelector('#main').querySelectorAll('pre[data-code-counter]').forEach(pre => {
83+
const env = {
84+
code: codeContainer.codes[pre.dataset.codeCounter],
85+
element: pre.querySelector(':scope > code')
86+
};
87+
88+
window.Prism.hooks.run('complete', env);
89+
});
90+
});
91+
}
92+
93+
window.$docsify.plugins = [].concat(install, window.$docsify.plugins);

docs/docsify/replace.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function install (hook) {
2+
let replacements;
3+
4+
hook.mounted(function () {
5+
replacements = window.$docsify.replacements || [];
6+
});
7+
8+
hook.beforeEach(function (content) {
9+
let modifiedContent = content;
10+
replacements.forEach(({ search, replace }) => {
11+
modifiedContent = modifiedContent.replace(search, replace);
12+
});
13+
return modifiedContent;
14+
});
15+
}
16+
17+
window.$docsify.plugins = [].concat(install, window.$docsify.plugins);

docs/examples/authentication_v5.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
2. Add documentation to the authentication service (`src/authentication.ts`).
3131
This example shows local authentication.
3232

33-
```typescript
33+
```typescript {"highlight": "12-16, 21-71", "lineNumbers": true}
3434
import { AuthenticationService, JWTStrategy } from '@feathersjs/authentication';
3535
import { LocalStrategy } from '@feathersjs/authentication-local';
3636
import type { Application } from './declarations';
@@ -117,10 +117,11 @@
117117
4. If you want to provide simple authentication usage on the SwaggerUI using Email/Username and Password,
118118
you can use the [Swagger UI Plugin ApiKeyAuthForm](https://github.com/Mairu/swagger-ui-apikey-auth-form).
119119

120-
Here is an example of an `openapi.ts` swagger configuration file, that can used with `api.configure()`;
120+
Here is an example of an `openapi.ts` swagger configuration file, that can used with `api.configure();`
121121

122122
```typescript
123-
import swagger, { FnSwaggerUiGetInitializerScript } from 'feathers-swagger';
123+
import swagger from 'feathers-swagger';
124+
import type { FnSwaggerUiGetInitializerScript } from 'feathers-swagger';
124125
import type { Application } from './declarations';
125126

126127
const getSwaggerInitializerScript: FnSwaggerUiGetInitializerScript = ({ docsJsonPath, ctx }) => {

docs/examples/custom_methods.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## Custom Methods <!-- {docsify-ignore} -->
22

33
### Code (from example app) <!-- {docsify-ignore} -->
4-
[filename](https://raw.githubusercontent.com/feathersjs-ecosystem/feathers-swagger/master/example/openapi-v3/customMethods.js ':include')
4+
[filename](https://raw.githubusercontent.com/feathersjs-ecosystem/feathers-swagger/{GITHUB_BRANCH}/example/openapi-v3/customMethods.js ':include')
55

66
### Preview in SwaggerUI <!-- {docsify-ignore} -->
77

docs/examples/generated_service_v5.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
1. Go into your `src/services/{$name}` folder, and open the service you want to edit `${name}.[tj]s`
44
2. Import the helper function and import the schemas (example for user service):
5-
```js
5+
```js {"highlight": "9-11", "lineNumbers": true}
66
import { createSwaggerServiceOptions } from 'feathers-swagger';
77
import {
88
userDataValidator,
@@ -11,14 +11,13 @@
1111
userDataResolver,
1212
userQueryResolver,
1313
userExternalResolver,
14-
// the lines below are added
1514
userDataSchema,
1615
userQuerySchema,
1716
userSchema,
1817
} from './users.schema';
1918
```
2019
adjust the options when the service is generated
21-
```js
20+
```js {"highlight": "6-12", "lineNumbers": true}
2221
app.use('users', new UserService(getOptions(app)), {
2322
// A list of all methods this service exposes externally
2423
methods: ['find', 'get', 'create', 'update', 'patch', 'remove'],

docs/examples/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Examples <!-- {docsify-ignore} -->
22

3-
In addition to the examples you can access from the navigation, you can check out the [example application](https://github.com/feathersjs-ecosystem/feathers-swagger/tree/master/example) of the git repository.
3+
In addition to the examples you can access from the navigation, you can check out the [example application](https://github.com/feathersjs-ecosystem/feathers-swagger/tree/{GITHUB_BRANCH}/example) of the git repository.
44

55
It contains many configurations and can be start with `npm start` when feathers-swagger have been checked out.
66

docs/getting-started.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
Add [OpenAPI](https://swagger.io/resources/open-api/) documentation to your Feathers services and optionally show them in the [Swagger UI](https://swagger.io/tools/swagger-ui/).
77

8-
You can also add custom methods to feathers services that will be available as rest routes but not via the feathers client.
8+
You can also add custom methods to feathers services that will be available as rest routes but not via feathers client.
99

1010
This version is prepared to work with Swagger UI >= 4.9
1111

docs/index.html

+31
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
99
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/docsify@4/lib/themes/vue.css">
1010
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/docsify-changelog-plugin@latest/dist/style.css">
11+
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/prismjs@1/plugins/line-highlight/prism-line-highlight.min.css">
12+
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/prismjs@1/plugins/line-numbers/prism-line-numbers.min.css">
1113
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
1214
<style>
1315
#CHANGELOG_RENDERER {
@@ -18,6 +20,25 @@
1820
iframe.swui-preview {
1921
height: 80vh;
2022
}
23+
24+
.line-highlight {
25+
margin-top: 3.5em;
26+
}
27+
28+
.markdown-section pre[data-line] {
29+
overflow: hidden;
30+
}
31+
32+
.markdown-section pre.line-numbers > code {
33+
overflow: visible;
34+
}
35+
36+
.markdown-section pre.line-numbers .line-numbers-rows {
37+
margin-top: 2.2em;
38+
}
39+
.markdown-section pre.line-numbers .line-highlight {
40+
margin-top: 1em;
41+
}
2142
</style>
2243
</head>
2344
<body>
@@ -32,13 +53,23 @@
3253
subMaxLevel: 3,
3354
loadNavbar: false,
3455
changelog: 'CHANGELOG.md',
56+
replacements: [
57+
{
58+
search: '{GITHUB_BRANCH}',
59+
replace: 'next'
60+
}
61+
],
3562
}
3663
</script>
3764
<!-- Docsify v4 -->
3865
<script src="//cdn.jsdelivr.net/npm/docsify@4"></script>
3966
<script src="//cdn.jsdelivr.net/npm/docsify-changelog-plugin@latest/dist/index.js"></script>
4067
<script src="//cdn.jsdelivr.net/npm/docsify-tabs@1"></script>
4168
<script src="//cdn.jsdelivr.net/npm/prismjs@1/components/prism-typescript.min.js"></script>
69+
<script src="//cdn.jsdelivr.net/npm/prismjs@1/plugins/line-highlight/prism-line-highlight.min.js"></script>
70+
<script src="//cdn.jsdelivr.net/npm/prismjs@1/plugins/line-numbers/prism-line-numbers.min.js"></script>
4271
<script src="//cdn.jsdelivr.net/npm/docsify@4/lib/plugins/search.min.js"></script>
72+
<script src="/docsify/prism-lines.js"></script>
73+
<script src="/docsify/replace.js"></script>
4374
</body>
4475
</html>

docs/migrations/MIGRATIONS_v1.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ swagger({
108108
### Generate RFC3986-compliant percent-encoded URIs
109109

110110
To generate valid specifications there may not be spaces in $ref links.
111-
Therefor concatenation is done with _ by default now. This applies to list and version refs of the previous version.
111+
Therefore concatenation is done with _ by default now. This applies to list and version refs of the previous version.
112112

113113
#### Before
114114
```js

docs/migrations/MIGRATIONS_v2.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ swagger({
5353
### docsPath option is removed and a default value for docsJsonPath has been added
5454

5555
As the uiIndex has been removed and the initialization of the ui has been extracted (to the swaggerUI initializer), the docsPath option has been removed / moved to the swaggerUI initializer.
56-
Therefor the docsJsonPath now has the default value of `/swagger.json`.
56+
Therefore the docsJsonPath now has the default value of `/swagger.json`.
5757

5858
### Default of openApiVersion option was changed from 2 to 3
5959

example/openapi-v3/customMethods.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = (app) => {
2323
customPost: swagger.customMethod('POST', '/do-post')((data, params) => {
2424
return Promise.resolve({ method: 'customPost', data, params });
2525
}),
26-
customPatchWithId: swagger.customMethod('PUT', '/do-patch-with/:__feathersId')(
26+
customUpdateWithId: swagger.customMethod('PUT', '/do-patch-with/:__feathersId')(
2727
(data, params, id) => {
2828
return Promise.resolve({ method: 'customPatchWithId', id, data, params });
2929
}
@@ -98,8 +98,8 @@ module.exports = (app) => {
9898
refs: {
9999
customPostRequest: 'custom_request',
100100
customPostResponse: 'custom_response',
101-
customPatchWithIdRequest: 'custom_request',
102-
customPatchWithIdResponse: 'custom_response'
101+
customUpdateWithIdRequest: 'custom_request',
102+
customUpdateWithIdResponse: 'custom_response'
103103
},
104104
operations: {
105105
find: {
@@ -190,6 +190,6 @@ module.exports = (app) => {
190190
.use(
191191
'/v3/custom-methods/:pathParamName/service',
192192
service,
193-
{ methods: ['find', 'customPost', 'customPatchWithId', 'customGetWithCustomIds'] }
193+
{ methods: ['find', 'customPost', 'customUpdateWithId', 'customGetWithCustomIds'] }
194194
);
195195
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"node": ">= 14"
2727
},
2828
"scripts": {
29-
"publish": "git push origin --tags && npm run changelog && git push origin",
29+
"publish": "git push origin --tags",
3030
"release:patch": "npm version patch && npm publish",
3131
"release:minor": "npm version minor && npm publish",
3232
"release:major": "npm version major && npm publish",

0 commit comments

Comments
 (0)