Code Monkey home page Code Monkey logo

typedoc-plugin-replace-text's Introduction

NPM Version Donate

typedoc-plugin-replace-text

This is a plugin for TypeDoc that replaces text in the documentation.

This includes:

  • Text in code comments (eg: method descriptions and method parameter descriptions)
  • Text in the main README that is used by TypeDoc
  • Text in included documents

You can specify matching patterns and the text they should be replaced with or a replacer function.

This can be useful for:

  • Creating links from ticket IDs (eg: replace "GH-12345" with a link to https://github.com/your-name/the-repo/issues/12345)
  • Creating links from author names (eg: link "Your Name" to your GitHub or corporate profile page)
  • Replacing internal URLs (domains) with external ones
  • Replacing custom placeholders with anything you like (eg: images or content from external files)
  • Removing URLs, user names, passwords or other text from your documentation
  • etc.

Installation

This module can be installed using npm:

$ npm install --save-dev typedoc-plugin-replace-text

Requirements

The plugin requires TypeDoc version 0.26.0 or above to be installed. You need to activate the plugin with the plugin option in your TypeDoc config.

After installation TypeDoc can be used normally and you can configure this plugin as described below.

Configuration

Extend your TypeDoc config file with a new option named replaceText. Here is an example using a JavaScript config file:

/** @type { import('typedoc').TypeDocOptionMap & import('typedoc-plugin-replace-text').Config } */
module.exports = {
    out: "output",
    entryPointStrategy: "expand",
    entryPoints: ["input/module1.ts", "input/module2.ts"],
    tsconfig: "tsconfig.json",
    readme: "MAIN.md",
    plugin: ["typedoc-plugin-replace-text"],
    replaceText: {
        inCodeCommentText: true,
        inCodeCommentTags: true,
        inMarkdown: false,
        replacements: [
            {
                pattern: "(GH-(\\d+))",
                replace: "[$1](https://github.com/your-name/the-repo/issues/$2)"
            },
            {
                pattern: "King Kong",
                flags: "gi",
                replace: function (match) {
                    if (this.sources?.[0].fileName.endsWith("/king-kong.ts")) {
                        return match + " is home!";
                    }
                    return match + " in another file!";
                },
            },
        ],
    },
};

Explanation:

Property Description
inCodeCommentText Specifies if the plugin should replace in the text of comments (not including the text of tags like the description of parameters for a method) in your code. (optional - defaults to true)
inCodeCommentTags Specifies if the plugin should replace in the text of tags (like the description of parameters for a method) in your code comments. (optional - defaults to true)
inMarkdown Specifies if the plugin should replace in all Markdown content parsed by TypeDoc (this includes the main README and all MD files added to the documentation). NOTE: Since version 0.26 TypeDoc parses all code comments as Markdown too. This means that setting this to true will automatically overwrite the other two options above to true. (optional - defaults to true)
replacements The search patterns and texts they should be replaced with. (pattern is the search Regex and flags are the optional Regex flags that default to g. replace can be a string constant or a replacer function that returns the new text for each match. The replacer function also has access to source information through the function context this - see example above where the sources property is an array of TypeDoc SourceReference objects.)

Bugs

Please report bugs here. Thanks for your contribution!

Donate

If you find this piece of software helpful, please consider a donation. Any amount is greatly appreciated.

Donate

typedoc-plugin-replace-text's People

Contributors

krisztianb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

typedoc-plugin-replace-text's Issues

Include typings in package

Hi, could you please export the typescript typings (or source) into dist/? This would help me with typing the configuration.

Currently, there are only JS files:
image

Thanks!

Option inIncludedFiles is too greedy

Using a plugin config like this doesn't produce the expected results:

"replaceText": {
    "inCodeCommentText": false,
    "inCodeCommentTags": false,
    "inIncludedFiles": true,
    "replacements": [
        {
            "pattern": "A",
            "replace": "B"
        }
    ]
}

Expected

Once would expect that A is replaced by B only in the README and other included markdown files as suggested by the plugin's README.

Actual

With the config above A is replaced everywhere by B even in code comment text and code comment tags.

Feature Request: Replace with contents from a file

I have a folder of examples in my project and I would like to use this plugin to replace references to these example files with the actual code, fx:
::--example: someExample.ts, 15, 32--::
Would become a code block containing lines 15-32 of the someExample.ts file.

The thing that is preventing me from accomplishing it with the current version of this plugin, is that it doesn't seem to support replacing snippets with the contents of other files.

Would you consider supporting this usecase with this plugin? Do you know other non-hacky ways of doing this?

Feature Request: Add path to current file in replacer function

I was hoping to use this package to replace the content of a comment with the contents of a file just like #4. I can get something to work using the replacer function, but in order to get the path to the file I want to read I would need the path to the current file being observed. For example, if I have a comment defined in a file located in src/my/location/Test.ts and a corresponding src/my/location/README.md I would like to do something like this:

/**
 * My test class.
 *
 * [[include ./README.md]]
 */
class Test {}

In order to resolve the location of ./README.md, I would need to know I'm located in src/my/location/Test.ts. It looks like the replacer function just gives info about the string match. Would it be feasible to add some more info about the match's file path to the replacer function?

Plugin doesn't work

Question

I have a very special project structure (It's not possible to change this structure) and there I have problems to get typodoc plugins installed.

Info

  • Angular Project with NX
  • I only want to document the lib modules
  • TypeDoc 0.24.6

Directory structure

myProject
--apps
----myProject
------tsconfig.json
--libs
----ui
------common
--------typedoc.json
--------tsconfig.json
------desktop
--------typedoc.json
--------tsconfig.json
----platform
------common
--------typedoc.json
--------tsconfig.json
------access
--------typedoc.json
--------tsconfig.json
--typedoc.json
--typedoc.base.json

Content of myProject\typedoc.base.json

{
	"$schema": "https://typedoc.org/schema.json",
	"includeVersion": true
}

Content of myProject\typedoc.json

{
	"basePath": "./",
	"entryPoints": [
		"libs/platform/*",
		"libs/ui/*"
	],
	"name": "Documentation",
	"entryPointStrategy": "packages",
	"out": "./dist/typedoc",
	"plugin": [
		"typedoc-umlclass",
		"typedoc-plugin-coverage",
		"typedoc-plugin-replace-text"
	],
	"umlClassDiagram": {
		"type": "detailed",
		"location": "local",
		"format": "svg",
		"umlClassDiagramVerboseOutput": true
	},
	"replaceText": {
		"inCodeCommentText": true,
		"inCodeCommentTags": true,
		"inIncludedFiles": true,
		"replacements": [
			{
				"pattern": "King Kong",
				"flags": "gi",
				"replace": "[King Kong](https://github.com/king-kong)"
			}
		]
	}
}

Content of myProject\libs\platform\common\typedoc.json (every typedoc.json in a nx-module looks the same)

{
	"extends": [
		"../../../typedoc.base.json"
	],
	"entryPoints": [
		"src/index.ts"
	]
}

Problem Description

When I run the command npx typedoc, the documentation is generated successfully. However, the string "King Kong" (I inserted it on various places, e.g. param description, class description etc) wasn't replaced.

But the plugin is loaded, which is also displayed during execution. The config entries also seem to be pulled, because if I make a spelling mistake on purpose, this would lead directly to the termination of the generation.

[info] Loaded plugin typedoc-plugin-replace-text

Does anyone knows what the problem here is?

This was the only way I got typedoc working. If there is a better way, I am open to using it. I tried to do everything from the instructions, but everything else didn't work. If any info is needed, I'll be happy to provide it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.