-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from cloudnc/refactor/compilable-markdown-codeb…
…locks docs(Readme): Add tooling to make readme embedded code written in typ…
- Loading branch information
Showing
7 changed files
with
120 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { existsSync, readFileSync, writeFileSync } from 'fs'; | ||
import { extname } from 'path'; | ||
|
||
/** | ||
* This simple script looks for code fences in source file for a syntax that looks like a file reference, optionally with a line number | ||
* reference. If a file exists at this location it is inserted into the code fence. | ||
* | ||
* Example: | ||
* | ||
* ```path/to/some/file.ts | ||
* ``` | ||
* | ||
* will look for path/to/some/file.ts and if present, read it and insert | ||
* | ||
* ```ts | ||
* // file content will appear hear | ||
* ``` | ||
* | ||
* ```path/to/some/file.ts#L10-50 | ||
* ``` | ||
* | ||
* ```ts | ||
* // file content (only lines 10 - 50) will appear hear | ||
* ``` | ||
* | ||
* | ||
*/ | ||
|
||
const [proc, thisFile, source, outputFile] = process.argv; | ||
|
||
const sourceText = readFileSync(source, 'utf-8'); | ||
|
||
const output = sourceText.replace(/```([\S]*)$\n([\s\S]*?)\n?```/gm, (substr: string, codeExtension: string) => { | ||
const matches = codeExtension.match(/\s?(\S+?)((#L(\d+)-L(\d+))|$)/m); | ||
|
||
if (!matches) { | ||
return substr; | ||
} | ||
|
||
const [_, filename, __, lineNumbering, startLine, endLine] = matches; | ||
|
||
if (!existsSync(filename)) { | ||
return substr; | ||
} | ||
|
||
const extension = extname(filename).slice(1); | ||
const file = readFileSync(filename, 'utf8'); | ||
|
||
let outputCode = file; | ||
|
||
if (lineNumbering) { | ||
const lines = file.split('\n'); | ||
|
||
outputCode = lines.slice(+startLine - 1, +endLine).join('\n'); | ||
} | ||
|
||
return ` | ||
\`\`\`${extension} | ||
${outputCode} | ||
\`\`\` | ||
`; | ||
}); | ||
|
||
console.log(output); | ||
writeFileSync(outputFile, output); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// hello-no-decorator.worker.ts | ||
import { Observable } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
import { runWorker, DoWork, GenericWorkerMessage } from 'observable-webworker'; | ||
|
||
class HelloWorker implements DoWork<string, string> { | ||
public work(input$: Observable<GenericWorkerMessage<string>>): Observable<GenericWorkerMessage<string>> { | ||
return input$.pipe( | ||
map(message => { | ||
console.log(message); // outputs 'Hello from main thread' | ||
return { | ||
payload: `Hello from webworker`, | ||
}; | ||
}), | ||
); | ||
} | ||
} | ||
|
||
runWorker(HelloWorker); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// hello.ts | ||
import { fromWorker } from 'observable-webworker'; | ||
import { of } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
|
||
const input$ = of({ payload: 'Hello from main thread' }); | ||
|
||
fromWorker<string, string>(() => new Worker('./hello.worker', { type: 'module' }), input$) | ||
.pipe(map(res => res.payload)) | ||
.subscribe(message => { | ||
console.log(message); // Outputs 'Hello from webworker' | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// hello.worker.ts | ||
|
||
import { ObservableWorker, DoWork, GenericWorkerMessage } from 'observable-webworker'; | ||
import { Observable } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
|
||
@ObservableWorker() | ||
class HelloWorker implements DoWork<string, string> { | ||
public work(input$: Observable<GenericWorkerMessage<string>>): Observable<GenericWorkerMessage<string>> { | ||
return input$.pipe( | ||
map(message => { | ||
console.log(message); // outputs 'Hello from main thread' | ||
return { | ||
payload: `Hello from webworker`, | ||
}; | ||
}), | ||
); | ||
} | ||
} |