# @nkduy/magic-string

> This library aims to provide codegen helpers and data structure for Kdu language plugin API v1.x that does not depend on Kocan runtime.

Latest version **0.2.2** (published 2023-06-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install @nkduy/magic-string
pnpm add @nkduy/magic-string
yarn add @nkduy/magic-string
bun add @nkduy/magic-string
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.2 |
| Published | 2023-06-14 |
| First published | 2023-06-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | nkduy |

## Links

- npm: https://www.npmjs.com/package/@nkduy/magic-string
- Repository: https://github.com/kocanjs/magic-string
- Homepage: https://github.com/kocanjs/magic-string#readme
- Issues: https://github.com/kocanjs/magic-string/issues
- npm.io page: https://npm.io/package/@nkduy/magic-string

## Recent versions

- 0.2.2 (latest) — 2023-06-14
- 0.2.1 — 2023-06-14
- 0.2.0 — 2023-06-14
- 0.1.0 — 2023-06-13

## README

This library aims to provide codegen helpers and data structure for Kdu language plugin API v1.x that does not depend on Kocan runtime.

## Usage

```html
<script>problems = 99</script>

<more-script lang="js">console.log( answer )</more-script>
```

```ts
import {
  toString,
  replace,
} from '@nkduy/magic-string';

/** @type {import('@kocan/kdu-language-core').KduLanguagePlugin} */
const plugin = () => {
	return {
		name: 'example-kdu-language-plugin',
		version: 1,
		resolveEmbeddedFile(fileName, sfc, embeddedFile) {
			if (embeddedFile.fileName.replace(fileName, '').match(/^\.(js|ts|jsx|tsx)$/)) {

        const s = embeddedFile.content;
        toString(s); // 'problems = 99'

				replace(s, 'problems', 'answer');
        toString(s); // 'answer = 99'

				replace(s, '99', '42');
        toString(s); // 'answer = 42'

        // add string by Array method directly
        s.unshift('var ');
        s.push(';');
        toString(s); // 'var answer = 42;'

        for (const block of sfc.customBlocks) {
          if (block.type === 'more-script') {
            s.push([
              block.content, // text to add
              block.name, // source
              0, // content offset in source
              {
                 // language capabilities to enable in this segment
                hover: true,
                references: true,
                definition: true,
                diagnostic: true,
                rename: true,
                completion: true,
                semanticTokens: true,
              },
            ]);
            toString(s); // 'var answer = 42;console.log( answer )'
          }
        }
      }
    }
  };
};
module.exports = plugin;
```

---
_Source: https://npm.io/package/@nkduy/magic-string · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
