Пример #1
0
        public void TestDefaultConfig()
        {
            var config = new SlugHelperConfiguration();

            Assert.Single(config.StringReplacements);
            Assert.Equal("-", config.StringReplacements[" "]);
        }
Пример #2
0
        public void TestDeniedCharacterConfig()
        {
            var config = new SlugHelperConfiguration
            {
                DeniedCharactersRegex = ""
            };

            Assert.Throws <InvalidOperationException>(() => config.AllowedChars);
        }
Пример #3
0
        public void TestEmptyConfig()
        {
            var config = new SlugHelperConfiguration();

            Assert.True(config.ForceLowerCase);
            Assert.True(config.CollapseWhiteSpace);
            Assert.Single(config.StringReplacements);
            Assert.Null(config.DeniedCharactersRegex);
            Assert.NotEmpty(config.AllowedChars);
        }
Пример #4
0
        public void TestRecursiveReplacement2()
        {
            const string original = "yababbabaz";
            const string expected = "yabbaz";

            var config = new SlugHelperConfiguration();

            config.StringReplacements.Add("abba", "");

            var helper = Create(config);

            Assert.Equal(expected, helper.GenerateSlug(original));
        }
Пример #5
0
        public void TestCharacterReplacementWithWhitespace()
        {
            const string original = "     abcde     ";
            const string expected = "bcde";

            var config = new SlugHelperConfiguration();

            config.StringReplacements.Add("a", " ");

            var helper = Create(config);

            Assert.Equal(expected, helper.GenerateSlug(original));
        }
Пример #6
0
        public void TestDeniedCharacterDeletionCustomized()
        {
            const string original = "ab!#$%&/()=";
            const string expected = "b$";

            var config = new SlugHelperConfiguration();

            config.AllowedChars.Remove('a');
            config.AllowedChars.Add('$');
            var helper = Create(config);

            Assert.Equal(expected, helper.GenerateSlug(original));
        }
Пример #7
0
        public void TestCharacterReplacementLookBackwards()
        {
            const string original = "cat";
            const string expected = "at";

            var config = new SlugHelperConfiguration();

            config.StringReplacements.Add("a", "c");
            config.StringReplacements.Add("cc", "a");

            var helper = Create(config);

            Assert.Equal(expected, helper.GenerateSlug(original));
        }
Пример #8
0
        public void TestCharacterReplacementLengthening()
        {
            const string original = "a";
            const string expected = "ccdccdcc";

            var config = new SlugHelperConfiguration();

            config.StringReplacements.Add("a", "bdbdb");
            config.StringReplacements.Add("b", "cc");

            var helper = Create(config);

            Assert.Equal(expected, helper.GenerateSlug(original));
        }
Пример #9
0
        public void TestCharacterReplacementDiacritics()
        {
            var config = new SlugHelperConfiguration();

            config.StringReplacements.Add("Å", "AA");
            config.StringReplacements.Add("å", "aa");
            config.StringReplacements.Add("Æ", "AE");
            config.StringReplacements.Add("æ", "ae");
            config.StringReplacements.Add("Ø", "OE");
            config.StringReplacements.Add("ø", "oe");

            var helper = Create(config);

            Assert.Equal("aa-aa-ae-ae-oe-oe", helper.GenerateSlug("Å å Æ æ Ø ø"));
        }
Пример #10
0
        public void TestCharacterReplacementShorting()
        {
            const string original = "catdogfish";
            const string expected = "cdf";

            var config = new SlugHelperConfiguration();

            config.StringReplacements.Add("cat", "c");
            config.StringReplacements.Add("dog", "d");
            config.StringReplacements.Add("fish", "f");

            var helper = Create(config);

            Assert.Equal(expected, helper.GenerateSlug(original));
        }
Пример #11
0
        public void TestCharacterReplacementUmlauts()
        {
            var config = new SlugHelperConfiguration()
            {
                StringReplacements =
                {
                    { "Ä", "Ae" },
                    { "Ö", "Oe" },
                    { "Ü", "Ue" },
                    { "ä", "ae" },
                    { "ö", "oe" },
                    { "ü", "ue" },
                    { "ß", "ss" }
                },
            };

            var helper = Create(config);

            Assert.Equal("aeoeueaeoeuess", helper.GenerateSlug("äöüÄÖÜß"));
        }
        private void UpdatePermalinks()
        {
            // SlugHelper.Config slugConfig = new SlugHelper.Config();
            var slugConfig = new SlugHelperConfiguration();

            // 从标题中删除问号(默认用破折号代替)
            slugConfig.StringReplacements.Add("?", "");
            SlugHelper helper = new SlugHelper();


            foreach (WordpressItem item in wordpressItems.Where(p => p.Type == "post" || p.Type == "page"))
            {
                if (recipeSettings.CreateRedirects)
                {
                    item.OldLink = urlCleaner.SanitiseRelativePath(item.Link);
                    item.Link    = $"/{DateTime.Parse(item.DatePublished).ToString(recipeSettings.PermalinkStructure)}/{helper.GenerateSlug(item.Title)}";
                }
                else
                {
                    item.Link = urlCleaner.SanitiseRelativePath(item.Link, true);
                }
            }
        }
Пример #13
0
 private static ISlugHelper Create(SlugHelperConfiguration config) => new SlugHelper(config);