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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 93 additions & 0 deletions
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

Lines changed: 17 additions & 0 deletions
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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)