Skip to content

Commit

Permalink
Fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulioRandall committed Jun 23, 2024
1 parent ed1b688 commit 4a4d061
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
26 changes: 11 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ Then `fileDocs` will be something like:
**To parse and clean nodes:**

```js
import p23, { cleanFileNode } from 'p23'
import p23, { cleanNodes } from 'p23'

const fileDocs = p23().map(cleanFileNode)
const files = p23()
const fileDocs = cleanNodes(files)
```

Note that cleaning doesn't alter whitespace. Because I have no idea what kind of whitespace formatting someone may use.
Cleaning removes the P23 delimiter and leading whitespace from lines. Whitespace filtering is opinionated:

An unbroken series of single line comments are the exception. The leading whitespace and prefix `//` is removed from each line. The leading space in the
content is also removed if there is one, and only one, leading space.
> TODO: Rules for whitespace filtering
```js
[
Expand All @@ -116,9 +116,9 @@ content is also removed if there is one, and only one, leading space.
eat: {
my: {
shorts: [`
A block node with multiple path segments.
A block node with multiple path segments.
`, `
Nodes with the same are presented in order as you'll see.
Nodes with the same are presented in order as you'll see.
`]
}
},
Expand All @@ -133,8 +133,8 @@ single line comments.`]
html: {
line: [` P23 will parse HTML comments too. `],
block: [`
That includes
multiline block comments.
That includes
multiline block comments.
`],
}
}
Expand All @@ -144,8 +144,8 @@ single line comments.`]

## Usage Notes

1. Doc strings include the comment delimters unless cleaned with `cleanFileNode` or by your own means.
2. Cleaning and managing the whitespace in node values is your responsibility.
1. Doc strings include the comment delimters unless cleaned with `cleanNodes` or by your own means.
2. Cleaning and managing other whitespace in node values is your responsibility.
3. Path segments must adhere to: `^[$@a-zA-Z_][$@a-zA-Z0-9_\-]*$`. This list may be extended in future to include **almost** any string character.
4. Nodes with the same name are in order of appearance within the file.
5. Yes, it will parse block comments in CSS nodes too.
Expand Down Expand Up @@ -179,7 +179,3 @@ I simply wanted to document a component's API within itself and regenerate that
A few documentation tools come close but none completely satisfy my need for simplicity, readability, flexibility, ability to document all mentioned aspects of the API. Furthermore, existing tools traded-off too much flexibility for conciseness. So I set about creating [**P24**](https://github.com/PaulioRandall/p24). In the process I was able to separate the concern of parsing annotated comments as this library, **P23**.

To clarify, **P23** is not about documenting components (**P24** does that). It is about specifying parsable comments within Svelte components. The output can then be used by a documentation package or some other innovative tooling. For example, you could build a changelog package where maintainers write changes to a component within the component. The package could render them in a similar manner to how **P24** does with API documentation.

## Fore Story

Version 2 could include support for multiple instances of the same node. The last specified value will be used if multiple nodes with the same name are parsed within a single component. The node values could go into an array instead, similar to query parameter parsing in some libraries.
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export { default as cleanNodes } from './node-cleaner.js'

import listFiles from './file-lister.js'
import loadContent from './file-reader.js'
import loadContent from './content-loader.js'
import parseNodes from './node-parser.js'

export { default as cleanNodes } from './node-cleaner.js'

export default (options = {}) => {
const files = listFiles(options)
const filesWithContent = loadContent(files)
Expand Down
22 changes: 13 additions & 9 deletions src/node-cleaner.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ const cleanJsLineValue = (s) => {
//
// three

s = s.trim()
const indent = parseIndent(s)
s = s.slice(indent.length)

s = removePrefix(s)
return removeCommentPrefixFromLines(s)
}

const removeCommentPrefixFromLines = (s) => {
s = s.trim()

const lines = s.split('\n')

for (let i = 0; i < lines.length; i++) {
for (let i = 1; i < lines.length; i++) {
// Remove the indent and comment delimter.
const linePrefix = lines[i].match(/^\s*\/\//)
if (linePrefix) {
Expand All @@ -77,7 +77,8 @@ const cleanJsBlockValue = (s) => {
/*p23.name value */

const indent = parseIndent(s)
s = s.trim()
s = s.slice(indent.length)

s = removePrefix(s)
s = removeSuffix(s, '*/')

Expand All @@ -87,14 +88,15 @@ const cleanJsBlockValue = (s) => {
* and sometimes like this
*/
s = removeJsBlockLineIndents(s, indent)
return s.trim()
return s
}

const cleanHtmlBlockValue = (s) => {
// <!--p23.name value -->

const indent = parseIndent(s)
s = s.trim()
s = s.slice(indent.length)

s = removePrefix(s)
s = removeSuffix(s, '-->')

Expand All @@ -106,7 +108,7 @@ const cleanHtmlBlockValue = (s) => {
-->
*/
s = removeHtmlBlockLineIndents(s, indent)
return s.trim()
return s
}

const parseIndent = (s) => {
Expand All @@ -133,8 +135,10 @@ const removeJsBlockLineIndents = (s, indent) => {

if (/^ \* /.test(lines[i])) {
lines[i] = lines[i].slice(3)
} else if (/^ /.test(lines[i])) {
} else if (/^\t/.test(lines[i])) {
lines[i] = lines[i].slice(1)
} else if (/^ /.test(lines[i])) {
lines[i] = lines[i].slice(2)
}
}

Expand Down
20 changes: 11 additions & 9 deletions src/node-cleaner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('node-cleaner.js', () => {
const exp = [
{
nodes: {
artist: ['Rhapsody of Fire'],
artist: [' Rhapsody of Fire'],
},
},
]
Expand All @@ -40,7 +40,7 @@ describe('node-cleaner.js', () => {
])

const expNode = lines(
'first', //
' first', //
'a',
' b',
' c'
Expand All @@ -60,9 +60,9 @@ describe('node-cleaner.js', () => {
test('JS block', () => {
const givenNode = lines(
' /*p23.artist first', //
' a',
' b',
' c',
' a',
' b',
' c',
' * d',
' e',
' */'
Expand All @@ -77,12 +77,13 @@ describe('node-cleaner.js', () => {
])

const expNode = lines(
'first', //
' first', //
'a',
' b',
' c',
'd',
' e'
'e',
''
)

const exp = [
Expand Down Expand Up @@ -115,11 +116,12 @@ describe('node-cleaner.js', () => {
])

const expNode = lines(
'first', //
' first', //
'a',
' b',
' c',
' e'
' e',
''
)

const exp = [
Expand Down

0 comments on commit 4a4d061

Please sign in to comment.