Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sync transform if w/ only explicit plugins #40

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ export default function remarkTextr(options) {
const settings = options || emptyOptions
const plugins = settings.plugins || []
const textrOptions = settings.options || {}

const needsPromise = plugins.some((plugin) => typeof plugin === 'string')

if (!needsPromise) {
const typography = textr(textrOptions).use(...plugins)

/**
* Transform synchronously.
*
* @param {Root} tree
* Tree.
* @returns {undefined}
* Nothing.
*/
return function (tree) {
visit(tree, 'text', function (node) {
node.value = typography.exec(node.value)
})
}
}

const promise = Promise.all(
plugins.map(async function (fn) {
if (typeof fn === 'string') {
Expand All @@ -60,7 +81,7 @@ export default function remarkTextr(options) {
})

/**
* Transform.
* Transform asynchronously.
*
* @param {Root} tree
* Tree.
Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ test('remarkTextr', async function (t) {
'yo «there» …\n'
)
})

t.test('should support syncronous transformer', function () {
assert.equal(
String(
remark()
.use(remarkTextr, {
plugins: [typographicQuotes],
options: {locale: 'ru'}
})
.processSync('yo "there" \n')
),
'yo «there»\n'
)
})
})

// Textr plugin: just a function to replace triple dots to ellipses.
Expand Down
Loading