sapegin / textlint-rule-title-case

Textlint rule to ensure that titles are using AP/APA style
MIT License
5 stars 2 forks source link

Get the header text/value from the children #1

Closed ggrossetie closed 3 years ago

ggrossetie commented 3 years ago

Currently, we are using a regular expression to extract the header text from the raw value:

https://github.com/sapegin/textlint-rule-title-case/blob/89e4cd698fd194a27a7eb54a0478e77332326cbb/index.js#L6

The issue is that the format # Title is specific to Markdown and won't work when using with an AsciiDoc document.

We can either update the regular expression to use either # or = but I think it would be better to extract the header text from the children no?

node {
  type: 'Header',
  depth: 1,
  children: [
    {
      type: 'Str',
      value: 'Document Title',
      loc: [Object],
      range: [Array],
      raw: 'Document Title'
    }
  ],
  loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 9 } },
  range: [ 0, 9 ],
  raw: '# Document Title'
}

const text = node.children
    .filter(child => child.type === 'Str')
    .map(child => child.value)
    .join(' ')
sapegin commented 3 years ago

That makes sense! Mind sending a pull request for that too? ;-)

ggrossetie commented 3 years ago

As mentioned by @azu we can use textlint-util-to-string:

I always use textlint-util-to-string for extracting text content from TxtParentNode. It picks each children nodes's value and joins these. (It aims to pick values that are displayed as rendering result)

https://github.com/opendevise/textlint-plugin-asciidoc/issues/1#issuecomment-753420572

@sapegin Do you have any preference?