Skip to content

Commit

Permalink
Remove wrapText
Browse files Browse the repository at this point in the history
This was originally added for GH-58, but `mdast-util-to-markdown`
(`remark-stringify`) now solves this, much better.
  • Loading branch information
wooorm committed Jan 11, 2023
1 parent 380b71b commit c2fab89
Show file tree
Hide file tree
Showing 26 changed files with 89 additions and 121 deletions.
7 changes: 3 additions & 4 deletions lib/handlers/br.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @typedef {import('hast').Element} Element
* @typedef {import('mdast').Break} Break
* @typedef {import('mdast').Text} Text
* @typedef {import('../types.js').State} State
*/

Expand All @@ -10,12 +9,12 @@
* State.
* @param {Element} node
* hast element to transform.
* @returns {Break | Text}
* @returns {Break}
* mdast node.
*/
export function br(state, node) {
/** @type {Break | Text} */
const result = state.wrapText ? {type: 'break'} : {type: 'text', value: ' '}
/** @type {Break} */
const result = {type: 'break'}
state.patch(node, result)
return result
}
3 changes: 1 addition & 2 deletions lib/handlers/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import {toText} from 'hast-util-to-text'
import {trimTrailingLines} from 'trim-trailing-lines'
import {wrapText} from '../util/wrap-text.js'

const prefix = 'language-'

Expand Down Expand Up @@ -59,7 +58,7 @@ export function code(state, node) {
type: 'code',
lang: lang || null,
meta: null,
value: trimTrailingLines(wrapText(state, toText(node)))
value: trimTrailingLines(toText(node))
}
state.patch(node, result)
return result
Expand Down
4 changes: 1 addition & 3 deletions lib/handlers/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* @typedef {import('../types.js').State} State
*/

import {wrapText} from '../util/wrap-text.js'

/**
* @param {State} state
* State.
Expand All @@ -18,7 +16,7 @@ export function comment(state, node) {
/** @type {HTML} */
const result = {
type: 'html',
value: '<!--' + wrapText(state, node.value) + '-->'
value: '<!--' + node.value + '-->'
}
state.patch(node, result)
return result
Expand Down
9 changes: 1 addition & 8 deletions lib/handlers/heading.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,14 @@ import {all} from '../all.js'
export function heading(state, node) {
/* c8 ignore next */
const depth = Number(node.tagName.charAt(1)) || 1
const wrap = state.wrapText

// To do: next major.
// To do: `mdast-util-to-markdown` should support breaks currently.
state.wrapText = false
const children = all(state, node)
state.wrapText = wrap

/** @type {Heading} */
const result = {
type: 'heading',
// @ts-expect-error: fine.
depth,
// @ts-expect-error: assume valid children.
children
children: all(state, node)
}
state.patch(node, result)
return result
Expand Down
3 changes: 1 addition & 2 deletions lib/handlers/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import {resolve} from '../util/resolve.js'
import {wrapText} from '../util/wrap-text.js'

/**
* @param {State} state
Expand All @@ -30,7 +29,7 @@ export function iframe(state, node) {
type: 'link',
title: null,
url: resolve(state, src),
children: [{type: 'text', value: wrapText(state, title)}]
children: [{type: 'text', value: title}]
}
state.patch(node, result)
return result
Expand Down
3 changes: 1 addition & 2 deletions lib/handlers/inline-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import {toText} from 'hast-util-to-text'
import {wrapText} from '../util/wrap-text.js'

/**
* @param {State} state
Expand All @@ -17,7 +16,7 @@ import {wrapText} from '../util/wrap-text.js'
*/
export function inlineCode(state, node) {
/** @type {InlineCode} */
const result = {type: 'inlineCode', value: wrapText(state, toText(node))}
const result = {type: 'inlineCode', value: toText(node)}
state.patch(node, result)
return result
}
25 changes: 8 additions & 17 deletions lib/handlers/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import {findSelectedOptions} from '../util/find-selected-options.js'
import {resolve} from '../util/resolve.js'
import {wrapText} from '../util/wrap-text.js'

const defaultChecked = '[x]'
const defaultUnchecked = '[ ]'
Expand Down Expand Up @@ -39,12 +38,9 @@ export function input(state, node) {
/** @type {Text} */
const result = {
type: 'text',
value: wrapText(
state,
properties.checked
? state.options.checked || defaultChecked
: state.options.unchecked || defaultUnchecked
)
value: properties.checked
? state.options.checked || defaultChecked
: state.options.unchecked || defaultUnchecked
}
state.patch(node, result)
return result
Expand All @@ -58,8 +54,8 @@ export function input(state, node) {
const result = {
type: 'image',
url: resolve(state, String(properties.src || '') || null),
title: wrapText(state, String(properties.title || '')) || null,
alt: wrapText(state, String(alt))
title: String(properties.title || '') || null,
alt: String(alt)
}
state.patch(node, result)
return result
Expand Down Expand Up @@ -111,13 +107,8 @@ export function input(state, node) {
const result = {
type: 'link',
title: null,
url: wrapText(
state,
properties.type === 'email' ? 'mailto:' + value : value
),
children: [
{type: 'text', value: wrapText(state, values[index][1] || value)}
]
url: properties.type === 'email' ? 'mailto:' + value : value,
children: [{type: 'text', value: values[index][1] || value}]
}

results.push(result)
Expand All @@ -143,7 +134,7 @@ export function input(state, node) {
}

/** @type {Text} */
const result = {type: 'text', value: wrapText(state, texts.join(', '))}
const result = {type: 'text', value: texts.join(', ')}
state.patch(node, result)
return result
}
3 changes: 1 addition & 2 deletions lib/handlers/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import {findSelectedOptions} from '../util/find-selected-options.js'
import {wrapText} from '../util/wrap-text.js'

/**
* @param {State} state
Expand All @@ -28,7 +27,7 @@ export function select(state, node) {

if (results.length > 0) {
/** @type {Text} */
const result = {type: 'text', value: wrapText(state, results.join(', '))}
const result = {type: 'text', value: results.join(', ')}
state.patch(node, result)
return result
}
Expand Down
3 changes: 0 additions & 3 deletions lib/handlers/table-cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import {all} from '../all.js'
* mdast node.
*/
export function tableCell(state, node) {
const wrap = state.wrapText
state.wrapText = false
const children = all(state, node)
state.wrapText = wrap

/** @type {TableCell} */
const result = {
Expand Down
3 changes: 1 addition & 2 deletions lib/handlers/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import {toText} from 'hast-util-to-text'
import {visit, SKIP} from 'unist-util-visit'
import {wrapText} from '../util/wrap-text.js'
import {all} from '../all.js'

/**
Expand All @@ -35,7 +34,7 @@ export function table(state, node) {
// Ignore nested tables.
if (state.inTable) {
/** @type {Text} */
const result = {type: 'text', value: wrapText(state, toText(node))}
const result = {type: 'text', value: toText(node)}
state.patch(node, result)
return result
}
Expand Down
4 changes: 1 addition & 3 deletions lib/handlers/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* @typedef {import('../types.js').State} State
*/

import {wrapText} from '../util/wrap-text.js'

/**
* @param {State} state
* State.
Expand All @@ -16,7 +14,7 @@ import {wrapText} from '../util/wrap-text.js'
*/
export function text(state, node) {
/** @type {MdastText} */
const result = {type: 'text', value: wrapText(state, node.value)}
const result = {type: 'text', value: node.value}
state.patch(node, result)
return result
}
3 changes: 1 addition & 2 deletions lib/handlers/textarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import {toText} from 'hast-util-to-text'
import {wrapText} from '../util/wrap-text.js'

/**
* @param {State} state
Expand All @@ -17,7 +16,7 @@ import {wrapText} from '../util/wrap-text.js'
*/
export function textarea(state, node) {
/** @type {Text} */
const result = {type: 'text', value: wrapText(state, toText(node))}
const result = {type: 'text', value: toText(node)}
state.patch(node, result)
return result
}
1 change: 0 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export function toMdast(tree, options) {
: handlers,
baseFound: false,
inTable: false,
wrapText: true,
frozenBaseUrl: undefined,
qNesting: 0
}
Expand Down
3 changes: 1 addition & 2 deletions lib/one.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/

import {all} from './all.js'
import {wrapText} from './util/wrap-text.js'

export const own = {}.hasOwnProperty

Expand Down Expand Up @@ -62,7 +61,7 @@ export function one(state, node, parent) {
function unknown(state, node) {
if ('value' in node && typeof node.value === 'string') {
/** @type {MdastContent} */
const result = {type: 'text', value: wrapText(state, node.value)}
const result = {type: 'text', value: node.value}
state.patch(node, result)
return result
}
Expand Down
2 changes: 0 additions & 2 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@
* Whether a `<base>` element was seen.
* @property {string | undefined} frozenBaseUrl
* `href` of `<base>`, if any.
* @property {boolean} wrapText
* To do: remove.
* @property {boolean} inTable
* Whether we’re in a table.
* @property {number} qNesting
Expand Down
3 changes: 1 addition & 2 deletions lib/util/find-selected-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/

import {toText} from 'hast-util-to-text'
import {wrapText} from './wrap-text.js'

/**
* @param {State} state
Expand Down Expand Up @@ -52,7 +51,7 @@ export function findSelectedOptions(state, node, properties) {
while (++index < max) {
const option = list[index]
const props = option.properties || {}
const content = wrapText(state, toText(option))
const content = toText(option)
const label = content || String(props.label || '')
const value = String(props.value || '') || content
values.push([value, label === value ? undefined : label])
Expand Down
15 changes: 0 additions & 15 deletions lib/util/wrap-text.js

This file was deleted.

6 changes: 4 additions & 2 deletions test/fixtures/br/index.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
alpha\
bravo

charlie
delta
```
charlie
delta
```

echo

Expand Down
20 changes: 11 additions & 9 deletions test/fixtures/figure-figcaption/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ Fig1. - A view of the pulpit rock in Norway.

Get browser details using navigator

function example() {
var txt;
txt = "Browser CodeName: " + navigator.appCodeName;
txt+= "Browser Name: " + navigator.appName;
txt+= "Browser Version: " + navigator.appVersion ;
txt+= "Cookies Enabled: " + navigator.cookieEnabled;
txt+= "Platform: " + navigator.platform;
txt+= "User-agent header: " + navigator.userAgent;
}
```
function example() {
var txt;
txt = "Browser CodeName: " + navigator.appCodeName;
txt+= "Browser Name: " + navigator.appName;
txt+= "Browser Version: " + navigator.appVersion ;
txt+= "Cookies Enabled: " + navigator.cookieEnabled;
txt+= "Platform: " + navigator.platform;
txt+= "User-agent header: " + navigator.userAgent;
}
```

Edsger Dijkstra:

Expand Down
4 changes: 1 addition & 3 deletions test/fixtures/listing/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ alpha();
bravo();
```


```
charlie delta(); echo
```
Expand All @@ -20,11 +19,10 @@ hotel(); india
```

```
```

```
juliett·
juliett
kilo();
lima
```
Loading

0 comments on commit c2fab89

Please sign in to comment.