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

Support for TOC snippet #5

Open
NicoJuicy opened this issue Nov 25, 2021 · 2 comments
Open

Support for TOC snippet #5

NicoJuicy opened this issue Nov 25, 2021 · 2 comments

Comments

@NicoJuicy
Copy link

NicoJuicy commented Nov 25, 2021

I created a ToC snippet and tried to run the application but ran into issues ( references and etc).

Is it possible to add support for the System.Text.RegularExpressions namespace?

The snippet i would like to add as a c# razor template is the following one:

@{

string text = Model.ActiveDocument.CurrentText;
string[] headerPrefix = new string[]{"- ","  * ","    + ","      - ","        * ","          + "};

string toc = "";

foreach(var line in text.Split("\n"))
{
	if(isHeader(line)){
		toc +=  GenerateTocLine(line) + "\n";
	}	
}


bool isHeader(string header) 
{
	int headerLevel = header.TakeWhile(c => c == '#').Count();
	return !(headerLevel == 0 || headerLevel > headerPrefix.Length); 
}

string GenerateTocLine(string header)
{
	int headerLevel = header.TakeWhile(c => c == '#').Count();

	return $"{headerPrefix[headerLevel -1]}[{getTitle(header, headerLevel)}]({GetLink(header, headerLevel)})";
}

string GetLink(string header,int headerLevel)
{
	string header1 = header.ToLowerInvariant().Replace("# ","#");
	header2 = Regex1.Replace(header, @"[^\w\s ]", "");
	header3 = header2.Replace(" ","-");
	return "#" + header3;
}

string getTitle(string header, int headerLevel)
{
	
	header = Regex.Replace(header, "#", "");
	return header.Trim();
}
}

@toc

When i added the regex dll to the folder, it found the namespace. But it displayed it with double ;; on the end. So the execution failed.

image

@RickStrahl
Copy link
Owner

You shouldn't need the dll since that's part of the .NET runtime.

I think all you should need is:

@using System.Text.RegularExpressions

Note the line has to be just like that, without a semi-colon at the top of the template.

I tried this with your code and I get a bunch of other errors, but not the double ;; and have the included System.Text.RegularExpressions.

@NicoJuicy
Copy link
Author

NicoJuicy commented Nov 25, 2021

I will check it out this evening, although this was the first thing i tried.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants